簡體   English   中英

Swift:選擇搜索結果后返回tableview

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

我已經成功實現了搜索功能。 我希望能夠搜索我的列表,選擇列表中的項目,然后在項目保持選中時返回主表視圖。 我該怎么做呢?

這是沒有在搜索欄中輸入任何選擇或字符的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)")
}

但是,我沒有成功地重新選擇主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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM