关于tableview的多选问题

声明下,虽然说的是原创,但是所有的方法都是谷歌搜到的,我只是整理者;

目前我发现有两种方法,第一种方法:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

当设置 tableview

[self.cardTableview setEditing:YES animated:YES];

就会出现多选的状态。

至于判断哪些被选中了,这个只能用两个方法来判断

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);

这种方法我不是很明白为什么会实现这种效果,但是事实就是可以实现,并且有人用这种方法也成功上架了,但是我不是很推荐这种方法,

总感觉怪怪的。


下面我推荐第二种方法,这个方法是苹果自己官网给的(点我进入苹果开发者中心),第二种方法如下:

self.cardTableview.allowsMultipleSelectionDuringEditing = YES;


然后设置tableview

[self.cardTableview setEditing:YES animated:YES];


同样会出现多选效果

获取被选中的cell

- (NSArray *)indexPathsForSelectedRows NS_AVAILABLE_IOS(5_0); 


设置多选的选择范围

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath*)indexPath {
    if (indexPath.row < 4) {
        return YES;
    } else {
        return NO;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // (...) configure cell

    if (indexPath.row < 4) {
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    } else {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
}



效果图如下:ios6




转载于:https://www.cnblogs.com/zjszyms/p/4119436.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值