[iPhone中级]iPhone实现自定义多选列表
来源:互联网 发布:广西网络推广168108cc 编辑:程序博客网 时间:2024/06/08 14:23
好久没更新博客了,今天写了一个自定义的多选列表,可以跟爱学习的各位进行分享,首先我们先来看一下效果图:
一般大家都是用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中级]iPhone实现自定义多选列表
- iPhone实现自定义多选列表
- iPhone实现自定义多选列表
- iPhone实现自定义多选列表
- [iPhone中级] IOS中实现自定义UICombox
- iphone 界面实现下拉列表
- iphone 界面实现下拉列表
- iphone UIPageControl实现自定义按钮
- QQ - iPhone 风格的好友列表实现
- Android 实现仿iphone下拉刷新列表
- iphone 界面如何实现下拉列表
- iphone界面如何实现下拉列表1
- iphone界面如何实现下拉列表2
- iPhone界面如何实现下拉列表
- 列表界面Iphone式平滑效果实现
- iphone界面如何实现下拉列表
- iPhone开发中UIPageControl实现自定义按钮
- iphone上自定义状态栏效果的实现
- C语言fgets函数了解
- 宝爸日记
- php字符串函数
- navicat_for_mysql_10.0.11在linux下的安装
- 技术人员少做老板梦
- [iPhone中级]iPhone实现自定义多选列表
- Lax Wendroff格式
- Linux上安装JDK1.7与Tomcat7.0
- 如何安装rpm软件包
- hdu 1898
- ubuntu更换163的源
- Hibernate持久化对象的三种状态
- Weblogic上修改类报异常:java.lang.ClassCastException
- jsp的内部方法jspInit(),_jspService(),jspDestroy()