ios - 搜索栏在 Swift 4 IOS App 中不起作用

标签 ios swift uisearchbar searchbar

我已经实现了一个搜索栏来搜索用户,但是当我搜索某些内容时,表格 View 中没有显示任何内容。我在下面附上了我的 View Controller 的图片。

View Controller :

View Controller

这个 View Controller 显示所有用户的列表,搜索栏应该帮助用户找到用户名。

import UIKit

class FindFriendsViewController: UIViewController {

var users = [User]()

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!

var searchItem = [String]()
var searching = false

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.tableFooterView = UIView()
    tableView.rowHeight = 71

    let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:)))
    tap.cancelsTouchesInView = false
    self.view.addGestureRecognizer(tap)
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    UserService.usersExcludingCurrentUser { [unowned self] (users) in
        self.users = users

        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }
}

extension FindFriendsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searching {
        return searchItem.count
    } else {
        return users.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "FindFriendsCell") as! FindFriendsCell

//让user = users[indexPath.row]

    var usernamesArr = [String]()
    for user in users {
        usernamesArr.append(user.username)
    }

    if searching {
        cell.textLabel?.text = searchItem[indexPath.row]
    } else {
        cell.textLabel?.text = usernamesArr[indexPath.row]
        cell.delegate = self
        configure(cell: cell, atIndexPath: indexPath)
    }

    return cell
}

func configure(cell: FindFriendsCell, atIndexPath indexPath: IndexPath) {
    let user = users[indexPath.row]

    cell.usernameLabel.text = user.username
    cell.followButton.isSelected = user.isFollowed
}

extension FindFriendsViewController: FindFriendsCellDelegate {
func didTapFollowButton(_ followButton: UIButton, on cell: FindFriendsCell) {
    guard let indexPath = tableView.indexPath(for: cell) else { return }

    followButton.isUserInteractionEnabled = false
    let followee = users[indexPath.row]

    FollowService.setIsFollowing(!followee.isFollowed, fromCurrentUserTo: followee) { (success) in
        defer {
            followButton.isUserInteractionEnabled = true
        }

        guard success else { return }

        followee.isFollowed = !followee.isFollowed
        self.tableView.reloadRows(at: [indexPath], with: .none)
    }
}

extension FindFriendsViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    var usernamesArr = [String]()
    for user in users {
        usernamesArr.append(user.username)
    }
    searchItem = usernamesArr.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
    searching = true
    tableView.reloadData()
}

最佳答案

我正在考虑您的代码中可能出现的不同问题。您需要设置搜索栏委托(delegate)和搜索结果更新程序:

yourSearchController.searchBar.delegate = self
yourSearchController.searchResultsUpdater = self

如果你没有 Controller ,直接搜索栏:

yourSearchBar.delegate = self
yourSearchBar.searchResultsUpdater = self

作为您的委托(delegate)人:

extension MasterViewController: UISearchBarDelegate {
  // MARK: - UISearchBar Delegate
  func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
    filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
  }
}

extension MasterViewController: UISearchResultsUpdating {
  // MARK: - UISearchResultsUpdating Delegate
  func updateSearchResults(for searchController: UISearchController) {
    let searchBar = searchController.searchBar
    let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
    filterContentForSearchText(searchController.searchBar.text!, scope: scope)
  }

或者您可能缺少更新之类的东西。检查排除问题的数据路径时间。首先,您在搜索栏中输入文本,然后检查文本所在的代码以及发生的情况。当搜索栏处于结束编辑状态时,您是否更新了表格 View ?

如果它对您没有帮助,请查看我之前学习过的精彩教程:Search Bar iOS

关于ios - 搜索栏在 Swift 4 IOS App 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55445156/

相关文章:

ios - 我想在 Objective C 中使用 MPAndroidChart swift 库

ios - 带有 UITableViewController 的 SearchBar 包含错误数量的单元格

ios - Objective-C - 改变控件的外观实现外观 api

ios - 用于搜索屏幕的 MVVM 和 RxSwift

ios - 按字母顺序和数字对数组进行排序

ios - 如何获取表格 View 单元格中每行字符串文本的高度?

ios - NSInteger 乘法 : Invalid operands to binary expression

ios - 特定缩放取决于注释

ios - 使用 OpenGL ES 2.0/cocos2d 2.0 绘制圆的最佳方法?

ios - 从另一个类将 Sprite 添加到 GameScene