Ios UITableViewCell将丢失高亮显示的选择
我正在使用Ios UITableViewCell将丢失高亮显示的选择,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在使用[tableview reloadData]在我的UItableView中重新加载数据,但是当我使用此选项时,我会在我的UItableVewCell上取消突出显示 我想知道恢复这一亮点的最佳方法 当用户选择他们编辑信息的单元格时,我设置了一个tempIndexPath,然后我调用reloadData,然后在cellforrowatinexpath中,我使用此代码重新突出显示单元格,但它不起作用 if ([tempIndexPath isEqual:indexPath]) {
[tableview reloadData]
在我的UItableView中重新加载数据,但是当我使用此选项时,我会在我的UItableVewCell上取消突出显示
我想知道恢复这一亮点的最佳方法
当用户选择他们编辑信息的单元格时,我设置了一个tempIndexPath,然后我调用reloadData,然后在cellforrowatinexpath
中,我使用此代码重新突出显示单元格,但它不起作用
if ([tempIndexPath isEqual:indexPath]) {
cell.selected = YES;
}
保存所选行,重新加载表视图的数据,然后再次选择该行
NSIndexPath* selectedIndexPath = [tableView indexPathForSelectedRow];
[tableView reloadData];
[tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
您应该知道这是危险的,因为如果在所选行之前将新项目添加到表视图中,所选内容将不是正确的单元格。您应该计算在行之前插入的行数,并相应地调整
selectedIndexPath
。此代码保留高亮显示的选择,并且重新选择/插入/重新定位是安全的,因为它保留了对模型中基础对象的引用,而不是索引路径。它还会滚动到所选内容,这在更新的模型导致所选内容移出当前滚动位置的帧时非常有用
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Save the selected object at this row for maintaining the highlight later in reloadData
_selectedItem = [_items objectAtIndex:indexPath.row];
}
- (void) reloadData
{
[_itemsTable reloadData];
//Reselect and scroll to selection
int numItems = _items.count;
for (int i = 0; i < numItems; i++) {
NSDictionary *dict = [_numItems objectAtIndex:i];
if (dict == _selectedItem) {
//This is more reliable than calling the indexPathForSelectedRow on the UITableView,
// since the selection is cleared after calling reloadData
NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
[_itemsTable scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
[_itemsTable selectRowAtIndexPath:selectedIndexPath animated:FALSE scrollPosition:UITableViewScrollPositionNone];
break;
}
}
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
{
//将选定对象保存在此行,以便稍后在“重新加载数据”中保持高亮显示
_selectedItem=[\u items objectAtIndex:indexPath.row];
}
-(void)重新加载数据
{
[_itemstablereloaddata];
//重新选择并滚动至选择
int numItems=\u items.count;
对于(int i=0;i
是的,我已经尝试过这段代码,它在显示旧值时引发了很多问题。但我会看看我能不能再解决它。