添加SearchBar会使某些TableViewCell无法点击

时间:2019-01-24 18:08:44

标签: ios swift uitableview uisearchbar

我的示例应用程序具有简单的TableView,其中几乎没有数据。我能够使用此配置选择所有单元格。当我在该tableView单元顶部添加SearchBar时出现问题。由于未知原因,我无法选择某些单元格。某些单元格无法点击会有些滞后。此ChatContactListVC是导航屏幕的一部分。我正在从Xib文件创建这些UITableViewCell。

class ChatContactListVC:UIViewController{

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

    var AppDelegate = UIApplication.shared.delegate as! AppDelegate
    lazy var chatManager = AppDelegate.chatManager!
    var contacts:[ChatModel] = [ChatModel]()
    var filterContact:[ChatModel] = [ChatModel]()

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

    override func viewDidLoad() {
        super.viewDidLoad()
        tableViewSetup()
        navigationItem.title = "New Message"
        searchBar.delegate = self
        searchBar.placeholder = "Search..."
    }

    func loadData(){
        chatManager.getFakeContactData { (data, err) in
            if err == nil{
                if let data = data {
                    self.contacts = data
                    self.filterContact = data
                    DispatchQueue.main.async {
                        self.tbView.reloadData()
                    }
                }
            }
        }
    }
    func tableViewSetup(){
        tbView.delegate = self
        tbView.dataSource = self
        //tbView.allowsSelection = true
        //tbView.separatorStyle = .none
        tbView.register(UINib(nibName: "ChatFriendListCell", bundle: nil), forCellReuseIdentifier: "cellId")
    }
}

extension ChatContactListVC:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filterContact.count
    }

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

        if filterContact.count != 0 {
            cell.configure(userName: filterContact[indexPath.row].userId, displayName: filterContact[indexPath.row].displayName, imageLink: "")
        }else{
            cell.configure(userName: filterContact[indexPath.row].userId, displayName: filterContact[indexPath.row].displayName, imageLink: "")
        }

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Didselect")
        print("selected")

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        var chatVC = storyboard.instantiateViewController(withIdentifier: "ChatViewController") as? ChatViewController
        chatVC?.chatManager = chatManager
        // change this
        chatVC?.toUserId = filterContact[indexPath.row].userId
        navigationController?.show(chatVC!, sender: nil)

        //self.present(chatVC!, animated: true, completion: nil)
    }



}

extension ChatContactListVC:UISearchBarDelegate{
    //MARK: SearchBar Methods
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        filterContact = searchText.isEmpty ? contacts : contacts.filter({ (data) -> Bool in
               data.displayName.lowercased().contains(searchText.lowercased()) || data.userId.lowercased().contains(searchText.lowercased())
        })
           self.tbView.reloadData()

    }
}

0 个答案:

没有答案