Swift TableView 中的搜索栏未在筛选的行中显示正确的数据

Swift SearchBar inTableView doesn't show the correct Data in the filtered rows

我的应用使用 Firebase 实时数据库来存储每个用户的信息。 tableView 工作正常。我添加了一个 searchBar,当我在 searchBar 中键入字母时,我可以看到 tableView 中显示的行数等于名称中包含这些字母的用户数。问题是表视图中显示的行(在搜索栏中输入字母后)包含第一个用户的信息,直到 x 个用户(x = 名称中包含这些字母的用户数量)。

import UIKit
import FirebaseDatabase
import Firebase

class VideoListScreen: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var blogPost: [BlogPost] = []
    var searchBlogPost = [BlogPost]()
    var searching = false

    override func viewDidLoad() {
        super.viewDidLoad()

        blogPost = []
        createArray()
        tableView.delegate = self
        tableView.dataSource = self

    }

    func createArray() {
        let ref = Database.database().reference().child("Users")

        ref.observe(.childAdded, with: { (snapshot) in
            if let postDict = snapshot.value as? [String : String] {
                let post = BlogPost(name:postDict["name"] ?? "" , gaf: postDict["gaf"] ?? "", place: postDict["place"] ?? "", phone: postDict["phone"] ?? "", notes: postDict["notes"] ?? "", elsertext: postDict["elsertext"] ?? "")

                self.blogPost.append(post)
                self.tableView.reloadData()
            }
        })
    }
}

extension VideoListScreen: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searching{
            return searchBlogPost.count
        }else{
           return blogPost.count
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let blogp = blogPost[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "Tavla") as! Tavla
        if searching{
            cell.setBLogPost(blogPost: searchBlogPost[indexPath.row])
        }
        else{
            cell.setBLogPost(blogPost: blogPost[indexPath.row])
        }
        cell.setBLogPost(blogPost: blogp)
        return cell
    }
}

extension VideoListScreen: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchBlogPost = blogPost.filter({[=11=].name.prefix(searchText.count) == searchText})
        searching = true
        tableView.reloadData()
    }
}

我认为问题出在这个函数的 if 和 else 语句中:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let blogp = blogPost[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "Tavla") as! Tavla
    if searching{
        cell.setBLogPost(blogPost: searchBlogPost[indexPath.row])
    }
    else{
        cell.setBLogPost(blogPost: blogPost[indexPath.row])
    }
    cell.setBLogPost(blogPost: blogp)
    return cell
}

您的委托方法

中有额外的cell.setBLogPost(blogPost: blogp)
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let blogp = blogPost[indexPath.row]
      let cell = tableView.dequeueReusableCell(withIdentifier: "Tavla") as! Tavla
      if searching{
          cell.setBLogPost(blogPost: searchBlogPost[indexPath.row])
      }
      else{
          cell.setBLogPost(blogPost: blogPost[indexPath.row])
      }

      return cell
  }

我认为这会解决您的问题,您可以使用

    import UIKit
import FirebaseDatabase
import Firebase

class VideoListScreen: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var blogPost: [BlogPost] = []
    var searchBlogPost = [BlogPost]()

    override func viewDidLoad() {
        super.viewDidLoad()

        blogPost = []
        createArray()
        tableView.delegate = self
        tableView.dataSource = self

    }

    func createArray() {
        let ref = Database.database().reference().child("Users")

        ref.observe(.childAdded, with: { (snapshot) in
            if let postDict = snapshot.value as? [String : String] {
                let post = BlogPost(name:postDict["name"] ?? "" , gaf: postDict["gaf"] ?? "", place: postDict["place"] ?? "", phone: postDict["phone"] ?? "", notes: postDict["notes"] ?? "", elsertext: postDict["elsertext"] ?? "")

                self.blogPost.append(post)

                self.searchBlogPost = self.blogPost
                self.tableView.reloadData()
            }
        })
    }
}

extension VideoListScreen: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return searchBlogPost.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let blogp = searchBlogPost[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "Tavla") as! Tavla
        cell.setBLogPost(blogPost: blogp)
        return cell
    }
}

extension VideoListScreen: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        if ((searchText.trimmingCharacters(in: .whitespaces).isEmpty) {
          searchBlogPost = blogPost
        } else {
          searchBlogPost = blogPost.filter({[=11=].name.prefix(searchText.count) == searchText})
        }

        tableView.reloadData()
    }
}

希望对您有所帮助。