声明下,虽然说的是原创,但是所有的方法都是谷歌搜到的,我只是整理者;
目前我发现有两种方法,第一种方法:
- (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;
[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;
}
}