2015-04-01 64 views
4

我已經成功實現了搜索功能。我希望能夠搜索我的列表,選擇列表中的一個項目,然後返回到主表格視圖,而項目仍然選擇。我該怎麼做呢?Swift:選擇搜索結果後返回tableview

這是沒有任何選擇或字符鍵入到搜索欄的tableview。項目沒有詳細視圖。項目確實具有可以檢索的更多信息,例如,網址。稍後當用戶按下左上方的「郵件」按鈕時,必須檢索此數據。 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 
} 
+0

視圖控制器標籤添加到您的時候您放入視圖中,選擇單元格時存儲標籤,循環瀏覽表格視圖的子視圖,如果標籤是存儲的標籤,則選擇正確的單元格 – milo526 2015-04-01 13:46:24

+0

在此方法中,用戶使用哪個按鈕從搜索結果列表到fu我會列出嗎? – 2015-04-01 21:36:05

+0

我編輯了我的反應,我希望這能爲你澄清一些問題 – milo526 2015-04-02 13:42:17

回答

0

UITableView代表有一個功能tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath。這個函數get在被選中時被調用。
如果您收聽此功能並保存選定的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) 
+0

Milo,結果很有希望!我可以在搜索表格視圖中爲選定的行存儲不同的值(例如文本爲字符串,或indexPath爲NSIndexPath)。但是,我還無法重新選擇主表視圖中的行。我認爲這是因爲我需要將「聲明的變量」帶到一個新的函數中,而這似乎不起作用。上例中的代碼。你會如何解決這個問題? – 2015-04-04 19:56:54

+0

你已經提供了很少的(非精確的:P)代碼,所以我需要猜測一下。你可以在'var selected = o'類之外聲明一個變量,而不是'selected = messageTableView.indexPathsForSelectedRows()?. NSIndexPath'當你回到你的tableview'self.tlbView.selectRowAtIndexPath(選中,動畫:true,scrollPosition:.Top)' – milo526 2015-04-04 20:06:40

+0

哈哈,代碼已經啓動!看看你的回覆,我已經聲明rowToSelect爲var rowToSelect = NSIndexPath(forRow:0,inSection:0)在函數之外(但在PublicationTableViewController類中)。然而,一旦我在搜索視圖中選擇一行,rowToSelect的新值似乎不會被結轉。我想我誤解了如何繼承變量。 – 2015-04-04 20:09:47

0

如果你能夠拿在tableViewController細胞的指數,你可以只要你回到你的tableView使用self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: .Top)。即使您滾動表格,這也會使單元格像您的圖片一樣變灰。

+0

在這種方法中,用戶使用哪個按鈕從搜索結果列表轉到完整列表? – 2015-04-01 21:36:52

+0

只要搜索欄爲空,他不需要額外的按鈕,如果tableview跳回到正常狀態(所有entrys)。我將使用委託,然後綁定'可選的func searchBar(_ searchBar:UISearchBar, textDidChange searchText:String)'請參閱:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/ UISearchBarDelegate_Protocol/index.html#// apple_ref/occ/intfm/UISearchBarDelegate/searchBar:textDidChange: – 2015-04-02 06:45:20