2015-06-14 22 views
0

我有一個tableview加載一個函數,並在viewDidAppear中顯示數據。當用戶點擊導航標題中的searchBar並搜索查詢時,將使用相同的tableview(我假設)。爲什麼使用UISearchResultsUpdating取消按鈕後,我的tableview空白?

問題出在用戶點擊取消按鈕後:調用原始數據的函數執行並打印正確的數據,但在tableView.reloadData之後屏幕上不顯示。

我試着將函數放在不同的地方(didCancel,didEndEditing),並且調用函數/正確地返回數據,但是沒有出現在表格中。

任何建議或解決方法?

class LocationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    tableView.delegate = self 
    tableView.dataSource = self 
} 

override func viewWillAppear(animated: Bool) { 
     self.load0() 
     tableView.reloadData() 

    self.locationSearchController = ({ 
     let controller = UISearchController(searchResultsController: nil) 
     controller.searchResultsUpdater = self 
     controller.hidesNavigationBarDuringPresentation = false 
     controller.dimsBackgroundDuringPresentation = false 
     controller.searchBar.delegate = self 
     controller.searchBar.searchBarStyle = .Minimal 
     controller.searchBar.sizeToFit() 
     self.navigationItem.titleView = controller.searchBar 
     return controller 
    })() 
} 

//below I try to remove the search data and reload the original data. 
override func viewDidDisappear(animated: Bool) { 
    self.locationSearchController.active = false 
} 

func searchBarTextDidBeginEditing(searchBar: UISearchBar) { 
    self.searchData.removeAllObjects() 
    self.category.removeAll(keepCapacity: false) 
    self.product.removeAll(keepCapacity: false) 
    self.users.removeAll(keepCapacity: false) 
self.load0() 
    tableView.reloadData() 
} 


func searchBarTextDidEndEditing(searchBar: UISearchBar) { 
    self.searchData.removeAllObjects() 
    self.category.removeAll(keepCapacity: false) 
    self.product.removeAll(keepCapacity: false) 
    self.users.removeAll(keepCapacity: false) 
self.load0() 
tableView.reloadData() 

} 

func searchBarCancelButtonClicked(searchBar: UISearchBar) { 
self.searchData.removeAllObjects() 
    self.category.removeAll(keepCapacity: false) 
    self.product.removeAll(keepCapacity: false) 
    self.users.removeAll(keepCapacity: false) 
     self.load0() 
     tableView.reloadData() } 

執行搜索功能是updateSearchResultsForSearchController:

func updateSearchResultsForSearchController(searchController: UISearchController) { query and places items in an array that's displayed by the original tableView } 

我想知道是否有一個的tableView正在使用,我是不知道的,但很顯然的是,searchController使用原始的tableView因爲它遵循我的可重用單元的設計。

+0

ü可以張貼一些代碼? –

回答

0

您必須填寫您的UITableView這樣:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if self.searchDisplayController!.active { 
      return self.filteredData.count 
     } 
     return self.defaultdData.count 
    } 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell 

     if self.searchDisplayController!.active { 
      cell.textLabel.text = self.filteredData[indexPath.row] 
     } else { 
      cell.textLabel.text = self.defaultData[indexPath.row] 
     } 

     return cell 
    } 
相關問題