ios - 在 tableView 中检索选定的行

标签 ios objective-c iphone uitableview

我正在创建一个 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/

相关文章:

ios - 测试时默认 undoManager 为 nil

ios - PFLoginViewController 不使用 Swift 显示 Facebook 登录按钮

ios - 确定国家多边形接触 MKMapView 的有效方法?

objective-c - App多处调用位置

objective-c - 去掉 "' NSObject' 可能不响应 '-someMethod'"警告

ios - 使用 UTC 时间获取 [NSDate 日期]

ios - iOS 是否提供内置的文本到语音支持或任何类似 NSSpeechRecognizer 的类?

objective-c - 如何检测 UITableViewCellStyleSubtitle 样式中 UITableViewCell 对象的 UIImageView 上的触摸

iphone - 如何创建一个自动调整大小的表格单元格来输入文本,就像在 iPhone 的 "mail"应用程序中一样?

ios - 获取联系人在 iphone 视网膜 4 英寸 64 位模拟器中不起作用,但在 3.5 英寸中工作正常