UISearchController采取错误的表视图

问题描述:

所以这是我的代码,我有视图控制器委托名为complexTable的表视图。出于某种原因,UISearchController查找self.tableView而不是self.complexTable。我无法找到我可以在哪里设置其表视图以匹配我的视图控制器委托的视图。 了解此代码的另一个重要说明是manager变量 - 它包含另一个Array<SidebarComplex>对象。UISearchController采取错误的表视图

最后,我想有一个命令,有点像这样:

self.searchDisplayController.table = self.complexTable 

,但我没有发现这样的事情。还有一些代表搜索栏的代表在storyboard中声明,但我并不真正了解他们所指的内容。

import UIKit 
let COMPLEX_IDENTIFIER = "SideBarComplexCell" 
class SideBarViewController: UIViewController { 


@IBOutlet var complexTable: UITableView! 

var complexSearchResults: Array<SidebarComplex> = [] 

var manager = SidebarManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    complexTable.dataSource = self 
    complexTable.delegate = self 
    complexTable.tableFooterView = UIView() 
    manager.fetch() 

    self.searchDisplayController!.delegate = self 
    self.complexTable.reloadData() 
} 

} 

extension SideBarViewController: UISearchBarDelegate, UISearchDisplayDelegate{ 


func filterContentForSearchText(searchText: String) { 

    self.complexSearchResults = self.manager.complexArray.filter({(complex: SidebarComplex) -> Bool in 
     return complex.name!.lowercaseString.rangeOfString(searchText.lowercaseString) != nil 
    }) 
} 


func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String?) -> Bool { 
    self.filterContentForSearchText(searchString!) 
    return true 
} 

} 

extension SideBarViewController: UITableViewDelegate, UITableViewDataSource{ 


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = complexTable.dequeueReusableCellWithIdentifier(COMPLEX_IDENTIFIER) as! SideBarComplexCell 

    var complexArray: Array<SidebarComplex> 
    if complexTable == self.searchDisplayController!.searchResultsTableView { 
     complexArray = self.complexSearchResults //This is never called even when I search. 
    } else { 
     complexArray = self.manager.complexArray 
    } 
    if complexArray.count > indexPath.section { 

     let complex = complexArray[indexPath.section] 
     cell.setupData(complex) 
     cell.setupUI() 
    } 

    return cell 

} 
func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

    if complexTable == self.searchDisplayController!.searchResultsTableView{ 
     print("took from search") //Never called!! 
     return self.complexSearchResults.count 
    } else { 
     print("took from all") 
     return self.manager.complexArray.count 
    } 
} 

} 

你需要比较的tableView与searchResultsTableView传递给函数 - 不是你的本地地产complexTable

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = complexTable.dequeueReusableCellWithIdentifier(COMPLEX_IDENTIFIER) as! SideBarComplexCell 

    var complexArray: Array<SidebarComplex> 
    if tableView == self.searchDisplayController!.searchResultsTableView { //<<<<<< HERE CHANGED 
     complexArray = self.complexSearchResults 
    } else { 
     complexArray = self.manager.complexArray 
    } 

    if complexArray.count > indexPath.section { 
     let complex = complexArray[indexPath.section] 
     cell.setupData(complex) 
     cell.setupUI() 
    } 
    return cell 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

    if tableView == self.searchDisplayController!.searchResultsTableView { //<<<<<< HERE CHANGED 
     return self.complexSearchResults.count 
    } else { 
     print("took from all") 
     return self.manager.complexArray.count 
    } 
} 
+1

perffffect!谢谢,我会接受解决:D – Eyzuky