简体   繁体   中英

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. Items do not have a detail view. Items do have more information that can be retrieved, eg 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. 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). 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. 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.
If you listen for this function and save the selected indexPath, you can use selectRowAtIndexPath to (re)select it in your main view.

Implement this function to listen for any selections made in your 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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