Swift: return 选择搜索结果后到tableview

Swift: return to tableview after selecting a search result

我已经成功实现了搜索功能。我希望能够搜索我的列表,select 列表中的一个项目,然后 return 到主表视图,而项目 保持 select编辑。我该怎么做?

这是没有在搜索栏中输入任何 select 离子或字符的表格视图。项目没有详细视图。项目确实有更多可以检索的信息,例如url。当用户按下左上角的 "mail" 按钮时,必须稍后检索此数据。

这是包含搜索结果的列表。单元格的灰色突出显示表示该单元格已 selected。我现在如何 return 到主表视图,同时保持 selection?我只看到右上角的取消按钮、搜索栏顶部中间的十字按钮和键盘右下角的 "search" 按钮。 None 在存储 selection 的同时将您带回主表视图。

根据建议的答案,我能够使用以下函数存储行的索引路径:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath       indexPath: NSIndexPath) {
    let rowToSelect = indexPath
    println(rowToSelect)
    selectedCellTitle = selectedCell?.textLabel?.text ?? ""
    println("The stored cell is called \(selectedCellTitle)")
}

但是,我没有成功地重新select主表视图中的行。我的代码如下。看起来常量 "rowToSelect" 没有转移到另一个函数(参见最后一行代码之前的那个)。我该如何解决?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        if tableView == self.searchDisplayController!.searchResultsTableView {
            cell.textLabel?.text = filteredPublications[indexPath.row].valueForKey("fullTitle") as? String
            cell.detailTextLabel?.text = filteredPublications[indexPath.row].valueForKey("journal") as? String
        } else {
            cell.textLabel?.text = publications[indexPath.row].valueForKey("fullTitle") as? String
            cell.detailTextLabel?.text = publications[indexPath.row].valueForKey("journal") as? String
            self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: .Top)
        }
return cell
}

如果您能够在 tableViewController 中保留 Cell 的索引,您可以在返回 table 时立即使用 self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: .Top) ]看法。即使您滚动 table.

,这也会使单元格像图片中一样保持灰色

UITableView Delegate 有一个函数tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath。当 selected 行时调用此函数。
如果您监听此函数并保存 selected indexPath,您可以在主视图中使用 selectRowAtIndexPath 来(重新)select 它。

实现此函数以侦听在您的 tableView

中生成的任何 selection
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //get back to your filled UITableView
    //Save "indexPath" to a variable
}

当您回到拥有 UITableView 的视图控制器时

self.tlbView.selectRowAtIndexPath(“above declared variable”, animated: true, scrollPosition: .Top)