【发布时间】:2016-05-09 03:51:16
【问题描述】:
我正在制作一个应用程序,目标 iOS 是 7.0。所以我使用搜索显示控制器。当我尝试搜索时,我提出了一个 api 请求,它的结果会晚一些,而不是搜索显示控制器更新表格视图。所以它是空的,虽然我有搜索结果。我尝试过类似
self.searchDisplayController?.searchResultsTableView.reloadData()
从请求中获取数据后立即,但它不起作用。
这是我的逻辑:
func filterContextForSearchText(searchText: String) {
BooksWorker.searchForBooks(searchText) { foundBooks in
self.foundBooks = foundBooks
if BooksWorker.books != nil {
self.filteredBooks = BooksWorker.books.filter { book in
return (book.name?.lowercaseString.containsString(searchText.lowercaseString))!
}
}
self.searchDisplayController?.searchResultsTableView.reloadData()
}
}
func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String?) -> Bool {
isSearch = true
filterContextForSearchText(searchString!)
return true
}
我以这种方式更新我的 tableView:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearch {
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
return filteredBooks.count
} else {
if BooksWorker.books != nil {
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
return (BooksWorker.books?.count)!
} else {
showEmptyTableView()
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
return 0
}
}
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if isSearch {
let cell = tableView.dequeueReusableCellWithIdentifier(AppData.CellIdentifiers.UndownloadedBookCell) as! UndownloadedBookCell
print("making cell")
cell.setBook(foundBooks[indexPath.row])
cell.delegate = self
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(AppData.CellIdentifiers.BookCell) as! BookCell
cell.setBook(BooksWorker.books![indexPath.row])
return cell
}
}
有人有想法吗?
【问题讨论】:
-
你设置
searchResultsDataSource了吗? -
screenschot 在这里好吗? Biblioteca 是我的表格视图控制器
标签: ios uitableview ios7 uisearchdisplaycontroller