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

时间:2015-04-01 13:38:20

标签: ios uitableview swift uisearchbar

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

这是没有在搜索栏中输入任何选择或字符的表格视图。项目没有详细信息视图。项目确实有更多可以检索的信息,例如网址。当用户按下左上角的“邮件”按钮时,必须在以后检索此数据。 This is the tableview populated with a subset of the full list (pulled from a test database)

这是包含搜索结果的列表。单元格的灰色突出显示表示已选择单元格。我现在如何在保持选择的同时返回主桌面视图?我只看到右上方的取消按钮,搜索栏顶部中间的交叉按钮,以及键盘右下方的“搜索”按钮。在存储选择时,没有人会将您带回主视图。 This is the page with search results. How do I return to the 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)")
}

但是,我没有成功地重新选择主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
}

2 个答案:

答案 0 :(得分:0)

如果您能够在tableViewController中保存Cell的索引,则可以在返回tableView后立即使用self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: .Top)。即使您滚动表格,这也会使单元格保持灰色,就像在图片中一样。

答案 1 :(得分:0)

UITableView Delegate具有函数tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath。选择行时调用此函数 如果您侦听此功能并保存所选的indexPath,则可以使用selectRowAtIndexPath在主视图中(重新)选择它。

实现此功能以侦听在tableView

中所做的任何选择
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)
相关问题