ios - 使用 UISearchResults 过滤时出现问题

标签 ios swift uitableview swift3 uisearchbar

所以我有一个来自 API 的 friend 列表,我也尝试实现搜索功能。到目前为止,一切都很好,过滤也很好,但我的第一部分实际上是一个将我发送到另一个 VC 的按钮,令人恼火的是,当我尝试搜索 friend 列表时,该单元格总是出现。实际上,我希望在搜索 friend 时隐藏它。

我添加了一些图片以使其更清楚this is how first the VC shows enter image description here enter image description here

可以看出,第三张图清楚地显示了问题。

我的代码看起来像。

var friendList = [Conversations]()
var filteredFriends = [Conversations]()

 func numberOfSections(in tableView: UITableView) -> Int {
    return friendList.count + 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isFiltering() {
        return filteredFriends.count
    }
    if section == 0 {
        return 1
    } else if section == 1 {
        return friendList.count
    }
    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewGroupConversationCell") as! NewGroupConversationTableViewCell
        return cell
    } else if indexPath.section == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewConversationCell") as! NewConversationTableViewCell
        var friendList: Conversations

        if isFiltering() {
            friendList = filteredFriends[indexPath.row]

        } else {
            friendList = self.friendList[indexPath.row]
        }

        cell.populate(friendList)
        return cell
    }
    return UITableViewCell()
}

    func searchBarIsEmpty() -> Bool {
    // Returns true if the text is empty or nil
    return searchController.searchBar.text?.isEmpty ?? true
}

func filterContentForSearchText(_ searchText: String, scope: String = "All") {
    filteredFriends = friendList.filter({( friend : Conversations) -> Bool in
        return (friend.name?.lowercased().contains(searchText.lowercased()))!
    })

    mainTableView.reloadData()
}

func isFiltering() -> Bool {
    return searchController.isActive && !searchBarIsEmpty()
}

extension NewConversationViewController: UISearchResultsUpdating {
// MARK: - UISearchResultsUpdating Delegate
func updateSearchResults(for searchController: UISearchController) {
    filterContentForSearchText(searchController.searchBar.text!)
}

如果您需要更多解释,请告诉我。

最佳答案

你的 numberOfSection 等于 friend 的数量,这就是为什么你的操作按钮的数量等于 friend 的数量,试试

func numberOfSections(in tableView: UITableView) -> Int {
    return 2 // your button, and your friend list
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        if isFiltering() {
            return 0 // hide button
        }
        return 1
    } else if section == 1 {
        if isFiltering() {
            return filteredFriends.count
        }
        return friendList.count
    }
    return 0
}

关于ios - 使用 UISearchResults 过滤时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48825815/

相关文章:

ios - 如何使用 SocketIO 订阅套接字 channel ?

iOS 联系人未显示在桌面 View 中

ios - 从 UITableView 中删除一个 UITableViewCell

ios - 无法将类型 '()' 的值分配给类型 'CallbackBlock!'

Swift 3 Type 初始化导致泄漏

ios - 如何为 UITextView Swift 添加下划线/粗体/斜体能力

iphone - 触摸内容 View 中的图像时防止 UITableViewCell 突出显示

objective-c - 在 NSDictionary 中获取包含 @ 字符的键的 valueForKey 时出错

ios - 在同一行上使用两次“滑动删除”功能时应用程序崩溃

ios - 在 Swift 2 中使用 NSDateComponents 计算下个月开始的奇怪日期结果