[英]tableview selection indication doesn't remain after reloadData when editing
I have a UITableView
in a UIViewController
. 我在
UIViewController
有一个UITableView
。 It's Selection modes are set to Single
and Multiple Selection During Editing
. Multiple Selection During Editing
,其选择模式设置为Single
和Multiple Selection During Editing
。
When I set the tableView's editing
property to YES
, if a process asynchronously runs reloadData
, any of my current check marks that have been selected disappear. 当我将tableView的
editing
属性设置为YES
,如果某个进程异步运行reloadData
,那么我当前选中的所有选中标记都将消失。 I implemented my tableView:cellForRowAtIndexPath:
method such that it sets the selected
property of the cell, based on what the selection was before the reloadData
. 我实现了
tableView:cellForRowAtIndexPath:
方法,以便根据reloadData
之前的选择设置单元格的selected
属性。 I've verified those are being set correctly. 我已经验证了这些被设置正确。 But to no avail, they still don't show up selected.
但无济于事,他们仍然没有出现选中。 What do I need to change so that it gets that set correctly?
我需要更改什么才能正确设置? It seems to retain the indented editing mode just fine.
似乎可以保留缩进编辑模式。
Here's said method: 这里说的方法:
- (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;
}
I use the didSelect and didDeselect methods to keep selections
(an NSMutableSet
) up to date. 我使用didSelect和didDeselect方法来使
selections
( NSMutableSet
)保持最新。 Eg 例如
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *section = self.sections[indexPath.section];
[self.selections removeObject: section[indexPath.row]];
}
Given the answers, I think I did not clarify which check marks I was referring to. 给定答案,我想我没有弄清楚我指的是哪个复选标记。 I am referring to the blue check marks that show a selected state on the left side of the table when the table has had its
editing
set to YES
as shown in this picture: 我指的是当表格的
editing
设置为YES
时,在表格左侧显示蓝色选中标记的蓝色复选标记,如下图所示:
If a reloadData
occurs, it will clear those blue check marks. 如果发生
reloadData
,它将清除那些蓝色的复选标记。
What I did to retain the visual selection status is to modify the KVO method that was firing the reloadData
to include code to programmatically reselect the current selections: 我要做的是保留可视选择状态,是修改触发
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];
}
}];
}];
}
...
The selectRowAtIndexPath:animated:scrollPosition:
method does the work. selectRowAtIndexPath:animated:scrollPosition:
方法可以完成工作。
So at this point, I guess my question is, do I really have to do this? 所以在这一点上,我想我的问题是,我真的必须这样做吗? Or I'm working too hard?
还是我太努力了?
Setting the cell to "selected" in cellForRowAtIndexPath
won't force a call to the UITableView
delegate methods so if that's where you're setting your checkmarks, they won't automatically appear upon the table view's reload. 在
cellForRowAtIndexPath
中将单元格设置为“ selected”将不会强制调用UITableView
委托方法,因此,如果您在此处设置选中标记,则它们不会在重新加载表视图时自动出现。
You can either add the checkmarks directly in cellForRowAtIndexPath
, ex: 您可以直接在
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;
}
Or you can call selectRowAtIndexPath
manually, ex: 或者,您可以手动调用
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.