2017-08-23 24 views
0

我刚刚完成向我的TableView添加搜索栏。除了搜索结果中显示的数据仍然显示原始未过滤数组中的对象(尽管当我单击结果时,它们会将我带到正在搜索的对象的正确DetailView中),它一切都完美无缺。xCode TableView搜索结果无法正确显示,因为cellForRowAt仍会调用原始的未过滤数组

所以搜索逻辑工作,我只是没有收到一个正确的视觉显示的搜索结果。

我认为这个问题出现在我的cellForRowAt函数中,我试图确定单元格是否填充了原始数组或搜索结果中的数据。这条线标有一个警告错误(黄色的!),它读取'初始化不可变值'位置'从未被使用;考虑替换为'_'或删除'。但是,我不确定如何写这行。

请参阅下面的相关搜索代码(使用注释标记错误行 - 它几乎在此代码的最底部)。

var locations = [Location]() 
var searchController:UISearchController! 
var searchResults = [Location]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    searchController = UISearchController(searchResultsController: nil) 
    tableView.tableHeaderView = searchController.searchBar 
    searchController.searchResultsUpdater = self 
    searchController.dimsBackgroundDuringPresentation = false 

    self.tableView.reloadData() 
} 

func filterContent(for searchText: String) { 

    searchResults = locations.filter({ (location) -> Bool in 
     if let name = location.name { 
      let isMatch = name.localizedCaseInsensitiveContains(searchText) 
      return isMatch 
     } 

     return false 

     }) 

} 


func updateSearchResults(for searchController: UISearchController) { 
    if let searchText = searchController.searchBar.text { 
     filterContent(for: searchText) 
     tableView.reloadData()   
    } 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if searchController != nil && searchController.isActive { 

     return searchResults.count 

    } else { 

     return locations.count 

    } 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cellIdentifier = "cell" 
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! LocationTableViewCell 

    // The following line is where I receive the error, as I attempt to determine whether the app should get the result from the filtered search result 
    let location = (searchController.isActive) ? searchResults[indexPath.row] : locations[indexPath.row] 

    cell.nameLabel.text = locations[indexPath.row].name 
    cell.thumbnailImageView.image = UIImage(named: locations[indexPath.row].image) 
    cell.locationLabel.text = locations[indexPath.row].location 
    cell.typeLabel.text = locations[indexPath.row].type 
    cell.accessoryType = locations[indexPath.row].isVisited ? .checkmark : .none 
    return cell 
} 

回答

2

你永远不会使用位置。将该代码更改为低于该行以使用它:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cellIdentifier = "cell" 
     let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! LocationTableViewCell 

     let location = (searchController.isActive) ? searchResults[indexPath.row] : locations[indexPath.row] 

     //use the location variable you just set to set the cell's properties 

     cell.nameLabel.text = location.name 
     cell.thumbnailImageView.image = UIImage(named: location.image) 
     cell.locationLabel.text = location.location 
     cell.typeLabel.text = location.type 
     cell.accessoryType = location.isVisited ? .checkmark : .none 
     return cell 
    }