2017-07-27 31 views
0

我有一個需要在其上搜索的uitableView。當我點擊一個單元格時,它會在該單元格中顯示一個指示器(這是多選表)。每件事情都能很好地發揮到這裏。但是,當我搜索某些內容並返回到原始表時,返回的索引不是原始索引,而是指示錯誤的單元格。我在stackoverflow中看到很多問題,但他們使用故事板或他們在Objective-c,我不明白。如何在過濾的uitableview swift中獲取原始的indexpath 3

如何在搜索表中獲取原始indexPath?

ContactListView

extension ContactListView: UITableViewDelegate, UITableViewDataSource { 
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     if (presenter?.isSelectionMode)! { 
      self.tableView.allowsMultipleSelection = true 
      let cell = tableView.dequeueReusableCell(withIdentifier: cellIdContactSelection, for: indexPath) as! ContactListCellSelection 
      cell.selectionStyle = .none 
      if searchController.isActive && searchController.searchBar.text != "" { 
       cell.dataSourceItem = filteredItems[indexPath.row] 
      } else { 
       cell.dataSourceItem = presenter?.items?[indexPath.item] 
      } 
      return cell 
     } 


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     if (presenter?.isSelectionMode)! { 
      let cell = tableView.cellForRow(at: indexPath) as? ContactListCellSelection 
      // configure decides to show indicator or not 
      configure(cell!, forRowAtIndexPath: indexPath) 
     } 
    } 

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if searchController.isActive && searchController.searchBar.text != "" { 
      return filteredItems.count 
     } 
     let count = presenter?.items?.count ?? 0 
     tableView.backgroundView = nil 
     if count == 0 { 
      tableView.EmptyMessage(title: NSLocalizedString("no_transaction_contact", comment: ""), message: NSLocalizedString("no_transaction_press_plus_button_contact", comment: "")) 
     } 
     return count 
    } 
func filterContentForSearchText(searchText: String) { 
     filteredItems = (presenter?.items?.filter { item in 
      return item.displayName.lowercased().contains(searchText.lowercased()) 
      })! 
     tableView.reloadData() 
    } 
} 
extension ContactListView: UISearchResultsUpdating { 
    @available(iOS 8.0, *) 
    func updateSearchResults(for searchController: UISearchController) { 
     filterContentForSearchText(searchText: searchController.searchBar.text!) 
    } 
} 
+0

將''rowIsSelected'''標誌添加到您的內存數據源中 – Wainage

+0

@ Wainage它不起作用。 –

+0

我必須。選中某行後,您會將「isSelected」添加到您收藏中的數據中。即使您過濾了該集合,該行的數據源也將具有「isSelected」變量。在表格重新呈現您檢查該標誌。不能簡單 – Wainage

回答

0

你的意思是,你選擇一個單元格,然後選擇另一cell.Andü要保存的細胞是最後選定單元格? 如果是這樣,你可以使用lastSelectedIndexPath(也許你想命名的其他東西)來標記最後選擇的單元格。

相關問題