Swift:选择搜索结果后返回tableview

Tit*_*zzi 6 uitableview uisearchbar ios swift

我已经成功实现了搜索功能.我希望能够搜索我的列表,选择列表中的项目,然后在项目保持选中时返回主表视图.我该怎么做呢?

这是没有在搜索栏中输入任何选择或字符的tableview.项目没有详细视图.项目确实有更多可以检索的信息,例如url.当用户按下左上角的"邮件"按钮时,必须稍后检索此数据. 这是填充了完整列表子集的tableview(从测试数据库中提取)

这是包含搜索结果的列表.单元格的灰色突出显示表示已选择单元格.我现在如何在保持选择的同时返回主桌面视图?我只看到右上方的取消按钮,搜索栏顶部中间的交叉按钮,以及键盘右下方的"搜索"按钮.在存储选择时,没有人带您回到主桌面. 这是包含搜索结果的页面. 我如何返回tableview?

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

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

但是,我没有成功地重新选择主tableview中的行.我的代码如下.看起来常量"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
}
Run Code Online (Sandbox Code Playgroud)

mil*_*526 0

UITableView Delegate 有一个函数tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath。当选择一行时调用此函数 get\xe2\x80\x99s。
\n如果您侦听此函数并保存所选的索引路径,则可以selectRowAtIndexPath在主视图中(重新)选择它。

\n\n

实现此函数来监听 tableView 中所做的任何选择

\n\n
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {\n    //get back to your filled UITableView\n    //Save "indexPath" to a variable\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
self.tlbView.selectRowAtIndexPath(\xe2\x80\x9cabove declared variable\xe2\x80\x9d, animated: true, scrollPosition: .Top)\n
Run Code Online (Sandbox Code Playgroud)\n