【问题标题】:Swift SearchBar inTableView doesn't show the correct Data in the filtered rowsSwift SearchBar inTableView 未在过滤的行中显示正确的数据
【发布时间】:2020-06-10 22:42:09
【问题描述】:

我的应用正在使用 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({$0.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
}

【问题讨论】:

    标签: uitableview uisearchbar uisearchbardelegate indexpath


    【解决方案1】:

    您的委托方法中有额外的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({$0.name.prefix(searchText.count) == searchText})
            }
    
            tableView.reloadData()
        }
    }
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多