[iPhone中级]iPhone实现自定义多选列表
2012-12-11 17:03
225 查看
好久没更新博客了,今天写了一个自定义的多选列表,可以跟爱学习的各位进行分享,首先我们先来看一下效果图:
![](http://img.my.csdn.net/uploads/201212/11/1355215547_9996.jpg)
一般大家都是用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];
在调用的页面,我们把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];
}
这里有很重要的一点就是委托方法的应用,如果前面一个页面需要后面页面的数据,就要想到用委托来实现回调,其实只要掌握了委托一些页面间的传值也就不难了,这篇文章只是给大家做个介绍,但如何做出自己想要的效果还是需要多多练习和学习的。谢谢
![](http://img.my.csdn.net/uploads/201212/11/1355215547_9996.jpg)
一般大家都是用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中级] IOS中实现自定义UICombox
- jquery自定义风格独特的下拉列表控件(可实现单选和多选)
- 用户对列表实现自定义排序实现方案
- 实现仿Iphone的带删除按钮的自定义的EditText
- Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)
- silverlight自定义用户控件,实现下拉多选功能
- Android编程实现自定义分享列表ACTION_SEND功能的方法
- Android-Dialog对话框 全解(普通对话框,单选对话框,多选对话框,列表对话框,自定义对话框,Activity对话框)
- Android高级控件(六)——自定义ListView高仿一个QQ可拖拽列表的实现
- checkBox多选支付列表的实现(可以跨屏幕实现多选并计算金额)
- Android自定义单选多选下拉列表
- vue v-model实现自定义样式多选与单选功能
- Infopath自定义表单实现列表字段联动
- 扩展Ext的Combobox实现多选下拉列表
- iphone游戏开发之cocos2d (七) 自定义精灵类,实现精灵动画CCAnimation
- iPhone之实现自定义进度条Progress