【问题标题】:search display controller update table view搜索显示控制器更新表格视图
【发布时间】: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


【解决方案1】:

你应该更新 tableView 而不是 searchDisplay self.tableView.reloadData()

【讨论】:

  • 没用,搜索结果还是空
  • 能调试filteredBooks.count看看有没有改吗?
  • 它正在改变。我在 searchForBooksclosure 中用 self.foundBooks.removeAll(); self.foundBooks.appendContentsOf(foundBooks) 替换了 self.foundBooks = foundBooks 并注意到了这个 sceenshot 。它在空 tableView 下显示空单元格,并显示“无结果”
  • 你能把searchDisplayController 的设置代表发到哪里吗?
  • 你检查过你是否从过滤器关闭中得到了什么吗?我附上了一个具有相同情况的项目示例,我正在使用超时计时器模拟 api 调用检查它应该可以帮助您找到解决方案mega.nz/#!DsUhFCCQ
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多