【问题标题】:Search with multiple selection swift快速多选搜索
【发布时间】:2019-01-31 19:56:06
【问题描述】:

我正在尝试同时实现 Tableview 多选和搜索。 多项选择很容易,但我希望在搜索时保持选中单元格。

我找不到任何同时实现两者的示例,所以如果您有任何示例或示例项目可以查看,请随时分享!

这是我的代码:

class InviteVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

var tableView: UITableView!

var phoneContacts = [PhoneContact]()
var selectedContacts = [PhoneContact]()
var filteredContacts = [PhoneContact]()
var rowToSelect = IndexPath(row: 0, section: 0)

var recipients = [String]()
var filter: ContactsFilter = .none

let searchController = UISearchController(searchResultsController: nil)

    func filterContentForSearchText(_ searchText: String) {
        filteredContacts = phoneContacts.filter({( contact : PhoneContact) -> Bool in
            return contact.name!.lowercased().contains(searchText.lowercased())
        })
        tableView.reloadData()
    }
}

extension InviteVC {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isFiltering() {
            return filteredContacts.count
        }
        return phoneContacts.count
    }

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



            let contact: PhoneContact



            if isFiltering() {
                contact = self.filteredContacts[indexPath.row]

            } else {
                contact = self.phoneContacts[indexPath.row]
            }

            cell.configure(contact)
            return cell
        }
        return UITableViewCell()
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let cell = tableView.cellForRow(at: indexPath) as! ContactCell
        cell.isSelected = true

        let contact: PhoneContact

        if isFiltering() {
            contact = self.filteredContacts[indexPath.row]
        } else {
            contact = self.phoneContacts[indexPath.row]
        }

        self.selectedContacts.insert(contact, at: 0)

    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! ContactCell
    }


    extension InviteVC: UISearchBarDelegate {
        // MARK: - UISearchBar Delegate
        func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
            filterContentForSearchText(searchBar.text!)


        }

        func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
            self.selectedContacts.removeAll()

            if let selectedItems = tableView.indexPathsForSelectedRows {

                for item in selectedItems {

                    tableView.deselectRow(at: item, animated: true)
                    let cell = tableView.cellForRow(at: item) as! ContactCell
                    cell.backgroundColor = .white
                    tableView.reloadData()
                }
            }
        }
    }

    extension InviteVC: UISearchResultsUpdating {

        // MARK: - UISearchResultsUpdating Delegate
        func updateSearchResults(for searchController: UISearchController) {
            let searchBar = searchController.searchBar
                if let searchText = searchBar.text,
                    !searchText.isEmpty {
                    filteredContacts.removeAll()
                }
            filterContentForSearchText(searchController.searchBar.text!)
        }
    }

【问题讨论】:

    标签: ios swift uitableview uisearchcontroller


    【解决方案1】:

    您不必在didSelectRowAt 方法中设置cell.isSelected = true。这将由 tableview 自动完成。

    保持选择的技巧是告诉 tableview 在重新加载单元格后选择一行。可能最好的放置位置是willDisplayCell

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    
            let contact = searchController.isActive ? filteredContacts[indexPath.row] : contacts[indexPath.row] //Update as needed
    
            if selectedContacts.contains(contact) {
                tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
            } else {
                tableView.deselectRow(at: indexPath, animated: false)
        }
    

    【讨论】:

    【解决方案2】:

    将此添加到您的cellForRowAtIndexPath 方法中,位于cell.configure(contact) 下方:

    cell.isSelected = self.selectedContacts.contains(contact)

    【讨论】:

    • 还是不行。完整代码在这里,请花点时间检查一下,让我知道你的想法:gist.github.com/Jkurbs/6172882220cea2205f02c9a03e0eb9fd
    • @John 你也可以添加PhoneContact 类定义吗?
    • 好的,我刚刚做了
    • @John,把cell.isSelected 到处设置。此外,您正在调用deselect 而不是select。见下文: let cell = tableView.cellForRow(at: indexPath) as! ContactCell cell.isSelected = false //去掉这个 if cell.isSelected { tableView.deselectRow(at: indexPath, animated: true) //这应该是tableView.selectRow ....你在调用取消选择行! }
    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 2015-03-20
    • 2021-02-10
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多