Поиск с множественным выбором Swift - PullRequest
0 голосов
/ 31 января 2019

Я пытаюсь реализовать множественный выбор 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!)
        }
    }

Ответы [ 2 ]

0 голосов
/ 31 января 2019

Вам не нужно устанавливать cell.isSelected = true в методе didSelectRowAt.Это будет сделано автоматически при просмотре таблицы.

Хитрость в сохранении выделения - это указание табличному виду выбирать строку после перезагрузки ячеек.Вероятно, лучшее место, чтобы положить его в 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)
    }
0 голосов
/ 31 января 2019

Добавьте это к вашему cellForRowAtIndexPath методу под cell.configure(contact):

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...