我正在使用带有复选框的 UITableView。在我的 cellForRowAtIndexPath
方法中,我将选定的几个单元格设置如下,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(tableView == self.sideBarTbl){
cell.textLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.tag = indexPath.row;
if(indexPath.row == 0){
[cell setSelected:true];
}else if(indexPath.row == 1){
[cell setSelected:true];
}else {
[cell setSelected:false];
}
}
简而言之,如果该行是 0,1,那么我检查 UITableView 的复选框。
当用户点击第 0 行或第 1 行时,它应该调用 didDeselectRowAtIndexPath
,因为第 0 行和第 1 行的复选标记已经被选中。但是,它总是调用 didSelectRowAtIndexPath
。不管复选框是否被选中,它总是调用didSelectRowAtIndexPath
。如何让它在选中复选框时调用 didDeselectRowAtIndexPath
并在未选中复选框时调用 didSelectRowAtIndexPath
?
didDeselectRowAtIndexPath
方法只会为之前被触摸选择的单元格调用。它与单元格的 selected
属性无关。如果你想对取消选择的单元格做一些事情,只需使用 didSelectRowAtIndexPath
方法在 NSMutableArray 中手动记录选择。忘记 didDeselectRowAtIndexPath
。供引用:UITableView cell on double click should go to previous state
还有一个 Objective-C 示例:
@interface SomeViewController : UIViewController<UITableViewDelegate> {
NSMutableArray *selectedIndexPaths;
}
@implementation SomeViewController
//......
- (void)viewDidLoad {
[super viewDidLoad];
selectedIndexPaths = [NSMutableArray array];
}
- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath {
if ([selectedIndexPaths containsObject:indexPath]) {
[selectedIndexPaths removeObject:indexPath];
//Do something when deselecting.
}
else{
[selectedIndexPaths addObject:indexPath];
//Do something when selecting.
}
}
//......
@end
如果选择是单选独占的,比如单选框,只需在处理前一个取消选择的过程后,使用单个变量NSIndexPath *selectedIndexPath;
在didSelectRowAtIndexPath
中记录选择选定的单元格。
关于ios - 当复选框被选中时,不会调用 didDeselectRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44340983/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) | Powered by Discuz! X3.4 |