列表的多选操作
来源:互联网 发布:数控编程的方法有哪些 编辑:程序博客网 时间:2024/06/09 20:10
在项目中我们会经常遇到列表多选与全选的操作,其实现的方式有很多,以下就是我的一种实现方法。多选就是两种状态:选中和未选中,其思路是
1,首先是初始化一个可变数组和可变字典,遍历出每个cell的数值,给每个cell都符值为“0”(“0”代表未选中,“1”代表选中),加在字典中,在把字典加在数组中。
2,其次是,在cell中取出数组中 的值做判断,当值为“0”时,显示“未选中”状态。值为“1”时,显示“选中状态”。
3,在点击方法中,当点击的值是“0”,设置值的状态改变为“1”,并且刷新着一行cell 数据。反之
注意:但你取出cell的row 是需要做字符串的转换。
下面是Dome:
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>{ NSMutableArray *arrDate; NSMutableArray *arrStauts; NSMutableDictionary *dic;}@property (nonatomic, strong) UITableView *tableView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; arrDate = [[NSMutableArray alloc] initWithObjects:@"苹果",@"梨子",@"香蕉",@"水果",@"蔬菜", nil]; _tableView = [[UITableView alloc] init]; _tableView.frame = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20); _tableView.dataSource = self; _tableView.delegate = self; [self.view addSubview:_tableView]; arrStauts = [[NSMutableArray alloc] init]; for (NSInteger i=0; i<arrDate.count; i++) { dic = [[NSMutableDictionary alloc] init]; [dic setObject:@"0" forKey:STR_NUM(i)]; [arrStauts addObject:dic]; }}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return arrDate.count;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 40;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"CELL"; TableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } cell.labTitle.text = arrDate[indexPath.row]; NSString *status = arrStauts[indexPath.row][STR_NUM(indexPath.row)]; if ([status integerValue] == 0) { cell.optionalImage.image = [UIImage imageNamed:@"weixuanzhon"]; } else { cell.optionalImage.image = [UIImage imageNamed:@"xuanzhon"]; } return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *status = arrStauts[indexPath.row][STR_NUM(indexPath.row)]; if ([status integerValue] == 0) { [dic setObject:@"1" forKey:STR_NUM(indexPath.row)]; [arrStauts replaceObjectAtIndex:indexPath.row withObject:dic]; } else { [dic setObject:@"0" forKey:STR_NUM(indexPath.row)]; [arrStauts replaceObjectAtIndex:indexPath.row withObject:dic]; } [self refreshCell:indexPath.row withSection:0];}- (void)refreshCell:(NSInteger )row withSection:(NSInteger)section{ [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:row inSection:section]] withRowAnimation:UITableViewRowAnimationNone];}
0 0
- 列表的多选操作
- 多选移动列表操作
- 顺序列表的操作
- 列表的一些操作
- 列表的常用操作
- 列表的基本操作
- javascript操作多选列表框
- javascript操作多选列表框
- DOM操作多选下拉列表
- Multiselect:操作多选列表框
- 08-DOM操作多选下拉列表
- multiselect插件操作多选列表框
- 下拉列表的相关操作
- 遍历操作列表的算法
- sharepoint 列表的基本操作
- jquery的全选列表操作
- python列表的快速操作
- 【python】列表的删除操作
- http conentType file separator
- PHP异步请求 POST和GET方式
- ORACLE 12C R2 RAC实战首发
- 本地socket(unix domain socket)
- 【iOS开发】Gitlab教程 (一)
- 列表的多选操作
- 关于判断输入框为空和出入空格的方法
- Android 面试基础知识整理
- Cursor window allocation of 2048 kb failed
- 北航软件991复试面试题
- Android 插件化之Droid Plugin
- 数学之美——隐含马尔科夫模型
- Android 目录过长造成的error Error:too long on Windows, keep below 240 characters :
- Emacs 移动行和区域move lines and regions with M-n M-p