我在 UIViewController
中有一个 UITableView
。它的选择模式设置为单选
和编辑期间的多选
。
当我将 tableView 的 editing
属性设置为 YES
时,如果一个进程异步运行 reloadData
,我当前的任何复选标记已经被选中消失。我实现了我的 tableView:cellForRowAtIndexPath:
方法,它根据 reloadData
之前的选择设置单元格的 selected
属性。我已验证这些正在设置正确。但无济于事,他们仍然没有出现被选中。我需要更改什么才能正确设置?它似乎很好地保留了缩进编辑模式。
方法如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ValveCell";
ValveCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Valve *valve = self.sections[indexPath.section][indexPath.row];
cell.subject = valve;
cell.selected = [self.selections member: valve];
return cell;
}
我使用 didSelect 和 didDeselect 方法使 selections
(NSMutableSet
)保持最新。例如
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *section = self.sections[indexPath.section];
[self.selections removeObject: section[indexPath.row]];
}
澄清
鉴于答案,我想我没有弄清楚我指的是哪个复选标记。我指的是当表格的 editing
设置为 YES
时表格的 left 显示选定状态的蓝色复选标记> 如图所示:
如果发生 reloadData
,它将清除那些蓝色复选标记。
更新:我目前的解决方案
为了保留视觉选择状态,我所做的是修改触发 reloadData
的 KVO 方法,以包含以编程方式重新选择当前选择的代码:
...
[self.tableView reloadData];
if (self.tableView.editing) {
[self.sections enumerateObjectsUsingBlock:^(id valves, NSUInteger section, BOOL *stop) {
[(NSArray*)valves enumerateObjectsUsingBlock:^(id valve, NSUInteger row, BOOL *stop) {
if ([self.selections member: valve]) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: row inSection: section];
[self.tableView selectRowAtIndexPath: indexPath animated: NO scrollPosition: UITableViewScrollPositionNone];
}
}];
}];
}
...
selectRowAtIndexPath:animated:scrollPosition:
方法完成这项工作。
所以在这一点上,我想我的问题是,我真的必须这样做吗?还是我工作太努力了?
最佳答案
在 cellForRowAtIndexPath
中将单元格设置为“选定”不会强制调用 UITableView
委托(delegate)方法,因此,如果您在其中设置复选标记,它们就会获胜'在表格 View 重新加载时自动出现。
您可以直接在 cellForRowAtIndexPath
中添加复选标记,例如:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ValveCell";
ValveCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Valve *valve = self.sections[indexPath.section][indexPath.row];
cell.subject = valve;
cell.selected = [self.selections member: valve];
if (cell.selected) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
或者您可以手动调用 selectRowAtIndexPath
,例如:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ValveCell";
ValveCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Valve *valve = self.sections[indexPath.section][indexPath.row];
cell.subject = valve;
cell.selected = [self.selections member: valve];
if (cell.selected) {
[self tableView:tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
return cell;
}
关于ios - 编辑时 reloadData 后 tableview 选择指示不会保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27138613/