-2
我有一个tableview控制器,我希望能够一次选择2个,并且只有2个单元格。如果您已经检查了2个单元格并且点击另一个未选中的单元格,则第1个单元格将取消选中,并且现在将检查最近的2个单元格。下面是我在用失败:有趣的循环行为?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
int count = 0;
int i = 0;
NSIndexPath *datPath = 0;
for (UITableViewCell *cell in [self.tableView visibleCells]) {
i++;
datPath = [NSIndexPath indexPathForRow:i inSection:0];
if([tableView cellForRowAtIndexPath:datPath].accessoryType == UITableViewCellAccessoryCheckmark){
count++;
}
}
if(count == 0){
firstChecked = indexPath;
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if(count == 1){
secondChecked = indexPath;
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if(count == 2){
[tableView cellForRowAtIndexPath:firstChecked].accessoryType = UITableViewCellAccessoryCheckmark;
firstChecked = secondChecked;
secondChecked = indexPath;
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
NSLog(@"Count is equal to: %d",count);
对于那些想知道,我有让我的表视图多重选择。以上代码会发生什么情况如下。伯爵不值钱,所以我可以检查3次,然后停下来让我检查。我错过了什么吗?谢谢。
当你有两个已经选择你不取消设置附件类型;你再次设置它'[tableView cellForRowAtIndexPath:firstChecked] .accessoryType = UITableViewCellAccessoryCheckmark;' –