好久没更新博客了,今天写了一个自定义的多选列表,可以跟爱学习的各位进行分享,首先我们先来看一下效果图:

一般大家都是用UITableView自己的编辑模式来实现CheckBox的,这里我们用自定义Cell和两张图片来实现,一张是未选中,一张是选中的图片

好了,我们首先来看一下代码:首先在Cell中定义了三个控件,两个UILabel和一个UIImageView

  1. #import <UIKit/UIKit.h>
  2. @interface MultiChoceCell : UITableViewCell
  3. @property (strong, nonatomic) IBOutlet UILabel *nameLabel;
  4. @property (strong, nonatomic) IBOutlet UILabel *departLable;
  5. @property (strong, nonatomic) IBOutlet UIImageView *checkBox;
  6. @end

好了,这个相信各位学过UITableViewCell的同学都应该知道,接下来,我们就来写最主要的ViewController了

在头文件.h文件里,我们首先定义了一个协议专门用来做回调的,还定义了一个选项数组和选中的数组,还有一个UITableView,我个人喜欢在ViewController里套上UITableView,因为可以改变TableView的大小。

  1. #import <UIKit/UIKit.h>
  2. @class MultiChoceViewController;
  3. @protocol MultiChoceDelegate <NSObject>
  4. @required
  5. -(void)MultiChoceSelectArray:(NSArray *)array ViewController:(MultiChoceViewController *)controller;
  6. @end
  7. @interface MultiChoceViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
  8. @property(nonatomic, strong)NSArray *itemArray;
  9. @property(nonatomic, strong)NSMutableArray *selectArray;
  10. @property (strong, nonatomic) IBOutlet UITableView *mTableView;
  11. @property (nonatomic, strong) id<MultiChoceDelegate> delegate;
  12. - (IBAction)backAction:(id)sender;
  13. - (IBAction)okAction:(id)sender;
  14. @end

接下来,我们看一下实现方式,在ViewDidLoad中将UITableView的颜色去掉,然后就是定义UITableViewDataSource和UITableViewDelegate了,看一下代码吧:

  1. #import "MultiChoceViewController.h"
  2. #import "MultiChoceCell.h"
  3. @interface MultiChoceViewController ()
  4. @end
  5. @implementation MultiChoceViewController
  6. - (void)viewDidLoad
  7. {
  8. [super viewDidLoad];
  9. [_mTableView setBackgroundColor:[UIColor clearColor]];
  10. // Do any additional setup after loading the view from its nib.
  11. }
  12. - (void)didReceiveMemoryWarning
  13. {
  14. [super didReceiveMemoryWarning];
  15. // Dispose of any resources that can be recreated.
  16. }
  17. #pragma mark UITableViewDataSource
  18. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  19. return [_itemArray count];
  20. }
  21. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  22. static NSString *identifier = @"itemCell";
  23. MultiChoceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  24. if (cell == nil) {
  25. NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MultiChoceCell" owner:self options:nil];
  26. cell = [array objectAtIndex:0];
  27. }
  28. NSDictionary *dict = [_itemArray objectAtIndex:indexPath.row];
  29. cell.nameLabel.text = [dict objectForKey:@"UserName"];
  30. cell.departLable.text = [dict objectForKey:@"DepartMent"];
  31. if ([_selectArray containsObject:dict]) {
  32. cell.checkBox.image = [UIImage imageNamed:@"checked.png"];
  33. }
  34. return cell;
  35. }
  36. - (IBAction)backAction:(id)sender {
  37. [self.navigationController popViewControllerAnimated:YES];
  38. }
  39. - (IBAction)okAction:(id)sender {
  40. [_delegate MultiChoceSelectArray:_selectArray ViewController:self];
  41. }
  42. #pragma mark UITableViewDelegate
  43. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  44. NSDictionary *selectDict = [_itemArray objectAtIndex:indexPath.row];
  1. //判断数据是否在选择列表中
  2. if ([_selectArray containsObject:selectDict]) {
  3. [_selectArray removeObject:selectDict];
  4. }else{
  5. [_selectArray addObject:selectDict];
  6. }
  7. [_mTableView reloadData];
  8. }
  9. @end

这里没什么特别的,只是在didSelectRowAtIndexPath中写了一句判断语句

在调用的页面,我们把itemArray和selectArray传入

  1. MultiChoceViewController *controlller = [[MultiChoceViewController alloc] initWithNibName:@"MultiChoceViewController" bundle:nil];
  2. controlller.delegate = self;
  3. controlller.itemArray = userArray;
  4. controlller.selectArray = selectArray;
  5. [self.navigationController pushViewController:controlller animated:YES];

并实现多选方法中中协议来关掉当前页面,并将选中的数据传出

  1. #pragma mark MultiChoceDelegate
  2. -(void)MultiChoceSelectArray:(NSMutableArray *)array ViewController:(MultiChoceViewController *)controller{
  3. selectArray = array;
  4. [self.navigationController popViewControllerAnimated:YES];
  5. }

这里有很重要的一点就是委托方法的应用,如果前面一个页面需要后面页面的数据,就要想到用委托来实现回调,其实只要掌握了委托一些页面间的传值也就不难了,这篇文章只是给大家做个介绍,但如何做出自己想要的效果还是需要多多练习和学习的。谢谢

iPhone实现自定义多选列表的更多相关文章

  1. WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...

  2. c# 自定义多选下拉列表2

    以下为工作中遇到的,备注一下 先需要几个辅助类 #region GripBounds using System.Drawing; internal struct GripBounds { ; ; pu ...

  3. 【转】WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 下拉选择控件ComboBox的自定义样式及扩展: 自定义多选控件Mul ...

  4. WPF 自定义ComboBox样式,自定义多选控件

    原文:WPF 自定义ComboBox样式,自定义多选控件 一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样 ...

  5. MVC应用程序与多选列表(checkbox list)

    原文:MVC应用程序与多选列表(checkbox list) 程序中,经常会使用checkbox lsit来呈现数.能让用户有多选项目.此博文Insus.NET练习的checkbox list相关各个 ...

  6. multiselect2side:jQuery多选列表框插件

    http://blog.csdn.net/rosanu_blog/article/details/8550723 http://www.bkjia.com/jQuery/449193.html < ...

  7. 自定义SWT控件二之自定义多选下拉框

    2.自定义下拉多选框 package com.view.control.select; import java.util.ArrayList; import java.util.HashMap; im ...

  8. Vue.js 多选列表(Multi-Select)组件

    搬运公众号早前文章 多选列表 (Multi-Select) 是一种将所有选项列出,并允许用户利用 Ctrl/Shift 键进行多选的 UI 元素.这是一种常见的设计元素.有时候为了节省空间,我们会将选 ...

  9. sharepoint 2010自定义访问日志列表设置移动终端否和客户端访问系统等计算列的公式

    上个月本人开发和上线了一个在SharePoint 2010上基于HTML5的移动OA网站,后端服务采用自定义的基于AgilePoint工作流引擎的Sharepoint Web服务,前端主要采用Jque ...

随机推荐

  1. SQL基本函数

    字符型函数 函数名称 描述 LOWER 将特定的字符串转化为小写,只影响字母字符串. UPPER 将整个字符串转换成大写,只影响字母字符串. INITCAP 将字符串中每一个单词的第一个字母转换为大写 ...

  2. android推荐使用dialogFrament而不是alertDialog

    DialogFragment在android 3.0时被引入.是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框.典型的用于:展示警告框,输入框,确认框等等. 在Dia ...

  3. matlab中的sub2ind函数

    在matlab中,矩阵的存储是按列优先,sub2ind函数将矩阵中指定元素的行列下标转换成存储的序号,即线性索引号.下面,我们举例子进行说明. 1 建立一个3*4*2的矩阵 rng(0,'twiste ...

  4. Java Math的 floor,round和ceil

    floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数 round方法,它表示"四舍五入",算法为Math.floor(x+0.5),即将原来的 ...

  5. 无刷新更新listview

    闲来无事,写点水文吧!有用得着的可以参考下,无刷新更新listview是什么意思呢?举个例子,在订单类listview列表中,常常会有各种订单状态,拿商城类app来说,会有待付款,待收货,确认收货等等 ...

  6. 1051. Pop Sequence (25)

    题目如下: Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N ...

  7. android notification,notificationmanager详解

    我们知道在使用Android的通知的时候一定会用到NotificationManager . Notification这两个类,这两个类的作用分别是: NotificationManager :  是 ...

  8. Mybatis执行Executor(一)

    在DefaultSqlSession中我们可以看到一系列的增删改查操作的其实都是在调用Executor的接口,Mybatis对外统一提供了一个操作接口类Executor,提供的接口方法有update. ...

  9. RabbitMQ消息队列(七):适用于云计算集群的远程调用(RPC)

            在云计算环境中,很多时候需要用它其他机器的计算资源,我们有可能会在接收到Message进行处理时,会把一部分计算任务分配到其他节点来完成.那么,RabbitMQ如何使用RPC呢?在本篇 ...

  10. Oracle WorkFlow(工作流)(二)

    2.4消息(Message) 消息主要是为通知服务的,可以把消息当作通知的内容和类型.消息也属于一个单据类型,通知只能和同一个单据类型里的消息相关联. 每个消息可以有一个或多个属性和自己相联系,消息的 ...