2017-05-08 25 views
0

所以這是我的代碼,我有視圖控制器委託名爲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 
    } 
} 

} 

回答

2

你需要比較的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