2015-06-14 9 views
0

У меня есть табличное представление, которое загружает функцию и отображает данные в viewDidAppear. Тот же tableview (я предполагаю) используется, когда пользователь нажимает на searchBar в заголовке навигации и ищет запрос.Почему мой табличный вид пуст после использования кнопки UISearchResultsUpdating cancel?

Проблема заключается в том, что пользователь отменяет кнопку отмены: функция для вызова исходных данных выполняет и печатает правильные данные, но не появляется на экране после таблицыView.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 
    }