![](/img/trans.png)
[英]Swift xcode IOS, return to tableview after selecting a search result
[英]Swift: return to tableview after selecting a search result
我已经成功实现了搜索功能。 我希望能够搜索我的列表,选择列表中的项目,然后在项目保持选中时返回主表视图。 我该怎么做呢?
这是没有在搜索栏中输入任何选择或字符的tableview。 项目没有详细信息视图。 项目确实有更多可以检索的信息,例如url。 当用户按下左上角的“邮件”按钮时,必须稍后检索此数据。
这是包含搜索结果的列表。 单元格的灰色突出显示表示已选择单元格。 我现在如何在保持选择的同时返回主桌面视图? 我只看到右上方的取消按钮,搜索栏顶部中间的交叉按钮,以及键盘右下方的“搜索”按钮。 在存储选择时,没有人带您回到主桌面。
基于建议的答案,我能够使用以下函数存储行的索引路径:
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
}
如果你能够在tableViewController中保存Cell的索引,那么只要你回到tableView就可以使用self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: .Top)
。 即使您滚动表格,这也会使单元格保持灰色,就像在图片中一样。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.