I have a UITableView that loads 1 cell per section, to provide spacing. When tapping on a cell, I can get the cell to appropriately expand. The problem is that other cells expand as well, and I cannot determine why. Multiple selection is not implemented. The GIF will illustrate my problem. My code is below.

Expands cells that are not selected

Code From View Controller

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)
    }

Thanks in advance!

Edit With Solution @Justin's suggestion was what caused the problem, the state of cell remained while being reused. I added the following to my custom cell and the problem is solved.

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

2 Answers

0
Justin Block On Best Solutions

Are you tracking the state of whether the cells been expanded or collapsed and using that in your cellForRowAt. My guess is as you scroll up and down it’s reusing cells and sometime’s it re-uses the cell that you’ve expanded so the other cells become expanded as well.

0
Harshal Pathak On
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)
    }

try to use this code, this may help you,
Happy Coding....