简体   繁体   English

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

[英]Swift: return to tableview after selecting a search result

I have successfully implemented a search function. 我已经成功实现了搜索功能。 I want to be able to search my list, select an item on the list, then return to the main tableview while the item remains selected. 我希望能够搜索我的列表,选择列表中的项目,然后在项目保持选中时返回主表视图。 How do I do this? 我该怎么做呢?

This is the tableview without any selections or character typed into the searchbar. 这是没有在搜索栏中输入任何选择或字符的tableview。 Items do not have a detail view. 项目没有详细信息视图。 Items do have more information that can be retrieved, eg url. 项目确实有更多可以检索的信息,例如url。 This data must be retrieved later when a user presses the "mail" button top left. 当用户按下左上角的“邮件”按钮时,必须稍后检索此数据。 这是填充了完整列表子集的tableview(从测试数据库中提取)

This is the list with search results. 这是包含搜索结果的列表。 The grey highlight of the cell indicates that the cell is selected. 单元格的灰色突出显示表示已选择单元格。 How do I now return to the main tableview, whilst keeping the selection? 我现在如何在保持选择的同时返回主桌面视图? I only see the cancel-button top right, the cross-button in the searchbar top middle, and the "search" button on the lower right part of the keyboard. 我只看到右上方的取消按钮,搜索栏顶部中间的交叉按钮,以及键盘右下方的“搜索”按钮。 None bring you back to the main tableview whilst storing the selection. 在存储选择时,没有人带您回到主桌面。 这是包含搜索结果的页面。我如何返回tableview?

Based on the suggested answers, I was able to store the row's index path, using the function below: 基于建议的答案,我能够使用以下函数存储行的索引路径:

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

However, I haven't succeeded in reselecting the row in the main tableview. 但是,我没有成功地重新选择主tableview中的行。 My code is below. 我的代码如下。 It looks like the constant "rowToSelect" is not carried over to another function (see the one before last line of code). 看起来常量“rowToSelect”没有被转移到另一个函数(参见最后一行代码之前的那个)。 How do I fix this? 我该如何解决?

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
}

If you're able to hold the index of the Cell in your tableViewController, you could use self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: .Top) as soon as you come back to your tableView. 如果你能够在tableViewController中保存Cell的索引,那么只要你回到tableView就可以使用self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: .Top) This will keep the cell grey like in your picture even if you scroll the table. 即使您滚动表格,这也会使单元格保持灰色,就像在图片中一样。

The UITableView Delegate has a function tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath . This function get's called when a row is selected. UITableView Delegate有一个函数tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath 。当选择一行时调用此函数。
If you listen for this function and save the selected indexPath, you can use selectRowAtIndexPath to (re)select it in your main view. 如果您侦听此功能并保存选定的indexPath,则可以使用selectRowAtIndexPath在主视图中(重新)选择它。

Implement this function to listen for any selections made in your tableView 实现此功能以侦听tableView中所做的任何选择

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //get back to your filled UITableView
    //Save "indexPath" to a variable
}

When you get back to the view controller where you have your UITableView 当您返回到具有UITableView的视图控制器时

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM