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

一般大家都是用UITableView自己的编辑模式来实现CheckBox的,这里我们用自定义Cell和两张图片来实现,一张是未选中,一张是选中的图片
好了,我们首先来看一下代码:首先在Cell中定义了三个控件,两个UILabel和一个UIImageView
- #import <UIKit/UIKit.h>
- @interface MultiChoceCell : UITableViewCell
- @property (strong, nonatomic) IBOutlet UILabel *nameLabel;
- @property (strong, nonatomic) IBOutlet UILabel *departLable;
- @property (strong, nonatomic) IBOutlet UIImageView *checkBox;
- @end
好了,这个相信各位学过UITableViewCell的同学都应该知道,接下来,我们就来写最主要的ViewController了
在头文件.h文件里,我们首先定义了一个协议专门用来做回调的,还定义了一个选项数组和选中的数组,还有一个UITableView,我个人喜欢在ViewController里套上UITableView,因为可以改变TableView的大小。
- #import <UIKit/UIKit.h>
- @class MultiChoceViewController;
- @protocol MultiChoceDelegate <NSObject>
- @required
- -(void)MultiChoceSelectArray:(NSArray *)array ViewController:(MultiChoceViewController *)controller;
- @end
- @interface MultiChoceViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
- @property(nonatomic, strong)NSArray *itemArray;
- @property(nonatomic, strong)NSMutableArray *selectArray;
- @property (strong, nonatomic) IBOutlet UITableView *mTableView;
- @property (nonatomic, strong) id<MultiChoceDelegate> delegate;
- - (IBAction)backAction:(id)sender;
- - (IBAction)okAction:(id)sender;
- @end
接下来,我们看一下实现方式,在ViewDidLoad中将UITableView的颜色去掉,然后就是定义UITableViewDataSource和UITableViewDelegate了,看一下代码吧:
- #import "MultiChoceViewController.h"
- #import "MultiChoceCell.h"
- @interface MultiChoceViewController ()
- @end
- @implementation MultiChoceViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [_mTableView setBackgroundColor:[UIColor clearColor]];
- // Do any additional setup after loading the view from its nib.
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return [_itemArray count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- static NSString *identifier = @"itemCell";
- MultiChoceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
- if (cell == nil) {
- NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MultiChoceCell" owner:self options:nil];
- cell = [array objectAtIndex:0];
- }
- NSDictionary *dict = [_itemArray objectAtIndex:indexPath.row];
- cell.nameLabel.text = [dict objectForKey:@"UserName"];
- cell.departLable.text = [dict objectForKey:@"DepartMent"];
- if ([_selectArray containsObject:dict]) {
- cell.checkBox.image = [UIImage imageNamed:@"checked.png"];
- }
- return cell;
- }
- - (IBAction)backAction:(id)sender {
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (IBAction)okAction:(id)sender {
- [_delegate MultiChoceSelectArray:_selectArray ViewController:self];
- }
- #pragma mark UITableViewDelegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- NSDictionary *selectDict = [_itemArray objectAtIndex:indexPath.row];
- //判断数据是否在选择列表中
- if ([_selectArray containsObject:selectDict]) {
- [_selectArray removeObject:selectDict];
- }else{
- [_selectArray addObject:selectDict];
- }
- [_mTableView reloadData];
- }
- @end
这里没什么特别的,只是在didSelectRowAtIndexPath中写了一句判断语句
在调用的页面,我们把itemArray和selectArray传入
- MultiChoceViewController *controlller = [[MultiChoceViewController alloc] initWithNibName:@"MultiChoceViewController" bundle:nil];
- controlller.delegate = self;
- controlller.itemArray = userArray;
- controlller.selectArray = selectArray;
- [self.navigationController pushViewController:controlller animated:YES];
并实现多选方法中中协议来关掉当前页面,并将选中的数据传出
- #pragma mark MultiChoceDelegate
- -(void)MultiChoceSelectArray:(NSMutableArray *)array ViewController:(MultiChoceViewController *)controller{
- selectArray = array;
- [self.navigationController popViewControllerAnimated:YES];
- }
这里有很重要的一点就是委托方法的应用,如果前面一个页面需要后面页面的数据,就要想到用委托来实现回调,其实只要掌握了委托一些页面间的传值也就不难了,这篇文章只是给大家做个介绍,但如何做出自己想要的效果还是需要多多练习和学习的。谢谢
iPhone实现自定义多选列表的更多相关文章
- WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...
- c# 自定义多选下拉列表2
以下为工作中遇到的,备注一下 先需要几个辅助类 #region GripBounds using System.Drawing; internal struct GripBounds { ; ; pu ...
- 【转】WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 下拉选择控件ComboBox的自定义样式及扩展: 自定义多选控件Mul ...
- WPF 自定义ComboBox样式,自定义多选控件
原文:WPF 自定义ComboBox样式,自定义多选控件 一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样 ...
- MVC应用程序与多选列表(checkbox list)
原文:MVC应用程序与多选列表(checkbox list) 程序中,经常会使用checkbox lsit来呈现数.能让用户有多选项目.此博文Insus.NET练习的checkbox list相关各个 ...
- multiselect2side:jQuery多选列表框插件
http://blog.csdn.net/rosanu_blog/article/details/8550723 http://www.bkjia.com/jQuery/449193.html < ...
- 自定义SWT控件二之自定义多选下拉框
2.自定义下拉多选框 package com.view.control.select; import java.util.ArrayList; import java.util.HashMap; im ...
- Vue.js 多选列表(Multi-Select)组件
搬运公众号早前文章 多选列表 (Multi-Select) 是一种将所有选项列出,并允许用户利用 Ctrl/Shift 键进行多选的 UI 元素.这是一种常见的设计元素.有时候为了节省空间,我们会将选 ...
- sharepoint 2010自定义访问日志列表设置移动终端否和客户端访问系统等计算列的公式
上个月本人开发和上线了一个在SharePoint 2010上基于HTML5的移动OA网站,后端服务采用自定义的基于AgilePoint工作流引擎的Sharepoint Web服务,前端主要采用Jque ...
随机推荐
- 学习Tensorflow,反卷积
在深度学习网络结构中,各个层的类别可以分为这几种:卷积层,全连接层,relu层,pool层和反卷积层等.目前,在像素级估计和端对端学习问题中,全卷积网络展现了他的优势,里面有个很重要的层,将卷积后的f ...
- 7.0、Android Studio命令行工具
命令行工具分成SDK工具和平台工具. SDK工具 SDK工具跟随SDK安装包安装并随时更新. Virtual Device 工具 1. Android Virtual Device Manager 提 ...
- UNIX网络编程——Socket粘包问题
一.两个简单概念长连接与短连接:1.长连接 Client方与Server方先建立通讯连接,连接建立后不断开, 然后再进行报文发送和接收. 2.短连接 Client方与Server每进行一次报文收发交易 ...
- Linux下which、whereis、locate、find 命令查找文件
转自:http://blog.csdn.net/gh320/article/details/17411743 我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索 ...
- NV12和NV21转rgb
void NV21_T_RGB(unsigned int width , unsigned int height , unsigned char *yuyv , unsigned char *rgb) ...
- Maven创建EJB
开发工具: eclipse mars wildfly jdk8 maven 右键新建project,选择other 勾选create simple project 填写信息(自行填写),完成后右键项目 ...
- Dynamics CRM 2013 SP1 客户表单界面上联系人subgrid上的添加现有联系人功能缺失
CRM2013打了SP1的同学会发现一个问题,客户关联联系人的1:N关系,在表单subgrid中添加联系人时,只能新建而无法添加现有联系人,而这个现象在之前的版本中是没有的. 我们通过工具ribbon ...
- Linux常用的网络命令
这些命令都是我在浏览网页的时候偶然看到的,但是不太完整,所以我就整理了一下,详见如下. 1.查看网络接口状态 ifconfig(interface configuration,接口配置),通常会加上- ...
- javascript语法之函数的定义
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 反对网抄,没有规则可以创建目标"install" 靠谱解答
在ubuntu下遇到这个问题,原因其实很简单,你不能用WINDWOS下的方法用图形方式打开,然后点了一下按扭"解压缩",生成了一个文件夹. 的确,这个文件夹看起来和正常的没有什么区 ...