android tableview实现多选功能,iOS开发之系统方式进行UITableView多选(不用数组维护选中状态)...

e ..一般多选 大家会想到使用一个自定义数组与indexPath进行关联。进行多选。进行ui变化。

其实UITableView已经自带了多选功能,状态由UITableView本身维护。代码很简单。直接上代码了。

一.设置可以多选

Objective-C

//非编辑模式下进行多选

self.tableView.allowsMultipleSelection = YES;

//编辑模式下也有对应方法

//self.tableView.allowsMultipleSelectionDuringEditing = YES;

1

2

3

4

//非编辑模式下进行多选

self.tableView.allowsMultipleSelection=YES;

//编辑模式下也有对应方法

//self.tableView.allowsMultipleSelectionDuringEditing = YES;

现在就可以进行多选啦。

二.设置选中后Cell的accessoryType或者自定义的图标。

Objective-C

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"SelectCell";

[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];

UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell.tintColor = [UIColor grayColor];

// cell.textLabel.textColor = self.titleColor?:[UIColor blackColor];

cell.textLabel.text = [[_newsArray objectAtIndex:indexPath.row] objectForKey:@"value"];

//防止重用后状态错乱.如果已经被选中,改变cell样式

if ([[tableView indexPathsForSelectedRows] containsObject:indexPath]) {

cell.accessoryType = UITableViewCellAccessoryCheckmark;

}else{

cell.accessoryType = UITableViewCellAccessoryNone;

}

return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//如果已经被选中,改变cell样式

UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

cell.accessoryType = UITableViewCellAccessoryCheckmark;

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

//如果取消选中,改变cell样式

UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

cell.accessoryType = UITableViewCellAccessoryNone;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{

staticNSString*CellIdentifier=@"SelectCell";

[tableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:CellIdentifier];

UITableViewCell*cell=(UITableViewCell*)[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

cell.tintColor=[UIColorgrayColor];

//    cell.textLabel.textColor = self.titleColor?:[UIColor blackColor];

cell.textLabel.text=[[_newsArrayobjectAtIndex:indexPath.row]objectForKey:@"value"];

//防止重用后状态错乱.如果已经被选中,改变cell样式

if([[tableViewindexPathsForSelectedRows]containsObject:indexPath]){

cell.accessoryType=UITableViewCellAccessoryCheckmark;

}else{

cell.accessoryType=UITableViewCellAccessoryNone;

}

returncell;

}

-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{

//如果已经被选中,改变cell样式

UITableViewCell*cell=(UITableViewCell*)[tableViewcellForRowAtIndexPath:indexPath];

cell.accessoryType=UITableViewCellAccessoryCheckmark;

}

-(void)tableView:(UITableView*)tableViewdidDeselectRowAtIndexPath:(NSIndexPath*)indexPath{

//如果取消选中,改变cell样式

UITableViewCell*cell=(UITableViewCell*)[tableViewcellForRowAtIndexPath:indexPath];

cell.accessoryType=UITableViewCellAccessoryNone;

}

三.获取选中后的所有行或者数据

self.tableView.indexPathsForSelectedRows 就行当前选中的所有的行,重用也不会使他数据错误。相当于我们自己维护的状态数组啦。

Objective-C

NSMutableArray *selectItems =[NSMutableArray array];

for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {

NSDictionary *item = [_newsArray objectAtIndex:indexPath.row];

[selectItems addObject:item];

}

1

2

3

4

5

NSMutableArray*selectItems=[NSMutableArrayarray];

for(NSIndexPath*indexPathinself.tableView.indexPathsForSelectedRows){

NSDictionary*item=[_newsArrayobjectAtIndex:indexPath.row];

[selectItemsaddObject:item];

}

Done!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值