我正在创建一个 TableView ,其中包含我从解析数据库中检索到的城市名称。我已经使用 didSelectRowAtIndexPath 和 cellForRowAtIndexPath 委托(delegate)方法实现了多项选择。当用户完成选定的城市后,我想将它们保存在解析数据库中,但是当用户回来时,我希望它从一开始就选择选定的城市。
我该如何实现?我是否需要将indexPath保存在我的解析数据库中,然后检索它,然后将其与tableView中的indexPaths或更好的解决方案进行比较?
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UserCell *cell = (UserCell *) [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [[self.filteredCandyArray objectAtIndex:indexPath.row] objectForKey:@"name"];
} else {
cell.textLabel.text = [[cityArray objectAtIndex:indexPath.row] objectForKey:@"name"];
}
if ([cellSelected containsObject:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//if you want only one cell to be selected use a local NSIndexPath property instead of array. and use the code below
//self.selectedIndexPath = indexPath;
//the below code will allow multiple selection
if ([cellSelected containsObject:indexPath])
{
[cellSelected removeObject:indexPath];
}
else
{
[cellSelected addObject:indexPath];
}
[tableView reloadData];
}
最佳答案
不是在 cellSelected
中保存 NSIndexPath
,而是保存城市名称。然后,一旦您再次返回此 TableView Controller ,您可以简单地获取选定的城市名称并填充 cellSelected
数组
关于ios - 在 tableView 中检索选定的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25324830/