iPhone实现自定义多选列表

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

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

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

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

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

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

[java]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @class MultiChoceViewController;  
  4.   
  5. @protocol MultiChoceDelegate <NSObject>  
  6.   
  7. @required  
  8.   
  9. -(void)MultiChoceSelectArray:(NSArray *)array ViewController:(MultiChoceViewController *)controller;  
  10.   
  11. @end  
  12.   
  13.   
  14. @interface MultiChoceViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>  
  15.   
  16. @property(nonatomic, strong)NSArray *itemArray;  
  17. @property(nonatomic, strong)NSMutableArray *selectArray;  
  18. @property (strong, nonatomic) IBOutlet UITableView *mTableView;  
  19. @property (nonatomic, strong) id<MultiChoceDelegate> delegate;  
  20.   
  21. - (IBAction)backAction:(id)sender;  
  22. - (IBAction)okAction:(id)sender;  
  23.   
  24. @end  
接下来,我们看一下实现方式,在ViewDidLoad中将UITableView的颜色去掉,然后就是定义UITableViewDataSource和UITableViewDelegate了,看一下代码吧:

[java]  view plain copy
  1. #import "MultiChoceViewController.h"  
  2. #import "MultiChoceCell.h"  
  3.   
  4. @interface MultiChoceViewController ()  
  5.   
  6. @end  
  7.   
  8. @implementation MultiChoceViewController  
  9.   
  10. - (void)viewDidLoad  
  11. {  
  12.     [super viewDidLoad];  
  13.       
  14.     [_mTableView setBackgroundColor:[UIColor clearColor]];  
  15.       
  16.     // Do any additional setup after loading the view from its nib.  
  17. }  
  18.   
  19. - (void)didReceiveMemoryWarning  
  20. {  
  21.     [super didReceiveMemoryWarning];  
  22.     // Dispose of any resources that can be recreated.  
  23. }  
  24.   
  25. #pragma mark UITableViewDataSource  
  26. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  27.       
  28.     return [_itemArray count];  
  29. }  
  30.   
  31.   
  32. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  33.       
  34.     static NSString *identifier = @"itemCell";  
  35.       
  36.     MultiChoceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];  
  37.       
  38.     if (cell == nil) {  
  39.         NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MultiChoceCell" owner:self options:nil];  
  40.           
  41.         cell = [array objectAtIndex:0];  
  42.     }  
  43.       
  44.     NSDictionary *dict = [_itemArray objectAtIndex:indexPath.row];  
  45.       
  46.     cell.nameLabel.text = [dict objectForKey:@"UserName"];  
  47.     cell.departLable.text = [dict objectForKey:@"DepartMent"];  
  48.       
  49.     if ([_selectArray containsObject:dict]) {  
  50.         cell.checkBox.image = [UIImage imageNamed:@"checked.png"];  
  51.     }  
  52.       
  53.     return cell;  
  54.   
  55. }  
  56.   
  57. - (IBAction)backAction:(id)sender {  
  58.       
  59.     [self.navigationController popViewControllerAnimated:YES];  
  60. }  
  61.   
  62. - (IBAction)okAction:(id)sender {  
  63.       
  64.     [_delegate MultiChoceSelectArray:_selectArray ViewController:self];  
  65.       
  66. }  
  67.   
  68. #pragma mark UITableViewDelegate  
  69. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  70.       
  71.     NSDictionary *selectDict = [_itemArray objectAtIndex:indexPath.row];  
[java]  view plain copy
  1.     //判断数据是否在选择列表中  
  2.     if ([_selectArray containsObject:selectDict]) {  
  3.         [_selectArray removeObject:selectDict];  
  4.     }else{  
  5.         [_selectArray addObject:selectDict];  
  6.     }  
  7.       
  8.     [_mTableView reloadData];  
  9. }  
  10. @end  
这里没什么特别的,只是在didSelectRowAtIndexPath中写了一句判断语句

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

[java]  view plain copy
  1. MultiChoceViewController *controlller = [[MultiChoceViewController alloc] initWithNibName:@"MultiChoceViewController" bundle:nil];  
  2.     controlller.delegate = self;  
  3.       
  4.     controlller.itemArray = userArray;  
  5.     controlller.selectArray = selectArray;  
  6.       
  7.     [self.navigationController pushViewController:controlller animated:YES];  
并实现多选方法中中协议来关掉当前页面,并将选中的数据传出

[java]  view plain copy
  1. #pragma mark MultiChoceDelegate  
  2. -(void)MultiChoceSelectArray:(NSMutableArray *)array ViewController:(MultiChoceViewController *)controller{  
  3.       
  4.     selectArray = array;  
  5.       
  6.     [self.navigationController popViewControllerAnimated:YES];  
  7.       
  8. }  
这里有很重要的一点就是委托方法的应用,如果前面一个页面需要后面页面的数据,就要想到用委托来实现回调,其实只要掌握了委托一些页面间的传值也就不难了,这篇文章只是给大家做个介绍,但如何做出自己想要的效果还是需要多多练习和学习的。谢谢
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
苹果提供了UIGestureRecognizer类,可以用来实现自定义手势。下面是一个简单的例子,实现在屏幕上绘制一个“V”形手势来触发事件: 1. 首先在视图控制器中创建一个手势识别器对象: ``` @property (nonatomic, strong) UIGestureRecognizer *gestureRecognizer; ``` 2. 在viewDidLoad方法中,创建手势识别器并将其添加到视图上: ``` self.gestureRecognizer = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; [self.view addGestureRecognizer:self.gestureRecognizer]; ``` 3. 实现手势处理方法,当手势被识别时,执行自定义的事件: ``` - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { NSArray *points = [gestureRecognizer valueForKey:@"touches"]; CGPoint startPoint = [[points firstObject] locationInView:self.view]; CGPoint endPoint = [[points lastObject] locationInView:self.view]; // 计算手势的方向 CGFloat dx = endPoint.x - startPoint.x; CGFloat dy = endPoint.y - startPoint.y; if (dx > 0 && dy < 0 && fabs(dx) > fabs(dy)) { // 手势为向右上方,执行自定义事件 [self doSomething]; } } } ``` 在上面的代码中,我们使用了touches属性获取手势的起点和终点,然后计算手势的方向,最后根据手势的方向执行自定义事件。 注意:在创建手势识别器时,需要指定手势的类型,例如: ``` UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeRecognizer]; ``` 上面的代码创建了一个向右滑动的手势识别器,并将其添加到视图上。当手势被识别时,会执行handleSwipe:方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值