UITableView непреднамеренный выбор нескольких ячеек - PullRequest
0 голосов
/ 23 апреля 2019

У меня есть UITableView, который загружает 1 ячейку на раздел, чтобы обеспечить интервал.При нажатии на ячейку, я могу заставить ячейку соответственно расширяться.Проблема в том, что расширяются и другие клетки, и я не могу определить, почему.Многократный выбор не реализован.GIF проиллюстрирует мою проблему.Мой код ниже.

Expands cells that are not selected

Код из контроллера вида

func numberOfSections(in tableView: UITableView) -> Int {
        return searchTextFieldIsEmpty() ? credentials.count : filteredCredentials.count
    }


    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 15 }


    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }


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



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

        if credentials.isEmpty {
            return cell
        } else {
            tableView.backgroundView = nil
            isFiltering() ? cell.formatCell(filteredCredentials[indexPath.section]) : cell.formatCell(credentials[indexPath.section])
            return cell
        }
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! CredCell
        cell.detailsView.isHidden = !cell.detailsView.isHidden

        let borderColor = UIColor(red: 206.0/255.0, green: 214.0/255.0, blue: 234.0/255.0, alpha: 1)
        cell.idView.addBorder(side: .bottom, thickness: 2, color: borderColor)
        cell.notificationView.addBorder(side: .bottom, thickness: 2, color: borderColor)
        cell.frontImageView.addBorder(side: .bottom, thickness: 2, color: borderColor)
        cell.backImageView.addBorder(side: .bottom, thickness: 2, color: borderColor)
        cell.notesView.addBorder(side: .bottom, thickness: 2, color: borderColor)
        tableView.beginUpdates()
        tableView.endUpdates()
        tableView.deselectRow(at: indexPath, animated: true)
        tableView.scrollToRow(at: indexPath, at: .none, animated: true)
    }

Заранее спасибо!

Редактировать с решением @ Джастин предположил, что причиной проблемы было состояние ячейки при повторном использовании.Я добавил следующее в свою пользовательскую ячейку, и проблема решена.

 override func prepareForReuse() {
        self.detailsView.isHidden = true
    }

Ответы [ 2 ]

0 голосов
/ 23 апреля 2019
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! CredCell
        cell.tag = indexPath.row
        cell.detailsView.isHidden = !cell.detailsView.isHidden

        if cell.tag == indexPath.row {
            let borderColor = UIColor(red: 206.0/255.0, green: 214.0/255.0, blue: 234.0/255.0, alpha: 1)
            cell.idView.addBorder(side: .bottom, thickness: 2, color: borderColor)
            cell.notificationView.addBorder(side: .bottom, thickness: 2, color: borderColor)
            cell.frontImageView.addBorder(side: .bottom, thickness: 2, color: borderColor)
            cell.backImageView.addBorder(side: .bottom, thickness: 2, color: borderColor)
            cell.notesView.addBorder(side: .bottom, thickness: 2, color: borderColor)
            tableView.beginUpdates()
            tableView.endUpdates()
        }
        tableView.deselectRow(at: indexPath, animated: true)
        tableView.scrollToRow(at: indexPath, at: .none, animated: true)
    }

попробуйте использовать этот код, это может помочь вам,
Happy Coding ....

0 голосов
/ 23 апреля 2019

Отслеживаете ли вы состояние, были ли ячейки расширены или свернуты, и используете это в своем cellForRowAt.Я предполагаю, что когда вы прокручиваете вверх и вниз, он повторно использует ячейки, и иногда он снова использует ячейку, которую вы расширили, так что другие ячейки также расширяются.

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