简体   繁体   中英

Swift Expandable tableView cell buttons are not appearing upon scrolling down

When scrolling off the screen with my custom cell in an expanded state the buttons are being hidden which should not be the case. Beneath the labels, 2 buttons are not appearing when scrolling up again. I'm guessing something needs to happen after the cell has been dequeued. Any help would be much appreciated.

在此处输入图像描述

I have a custom cell where initially the height for a row is 80, upon clicking on the cell it expands to 120. By default I have the buttons hidden like so:

    @IBOutlet weak var followButton: UIButton! {
        didSet {
            followButton.isHidden = true
        }
    }

    @IBOutlet weak var blockButton: UIButton! {
        didSet {
            blockButton.isHidden = true
        }
    }

I have an var expandedIndexSet: IndexSet = [] which tracks which cell has been expanded. The method below updates indexSet accordingly.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        tableView.beginUpdates()
        let cell = tableView.cellForRow(at: indexPath) as! StackOverflowTableViewCell

        if(expandedIndexSet.contains(indexPath.row)){
            expandedIndexSet.remove(indexPath.row)
        } else {
            expandedIndexSet.insert(indexPath.row)
        }

        cell.blockButton.isHidden = !cell.blockButton.isHidden
        cell.followButton.isHidden = !cell.followButton.isHidden

        tableView.endUpdates()
    }

The height gets adjusted like so:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if expandedIndexSet.contains(indexPath.row) {
            return 140
        }
        else {
            return 80
        }
    }

The following snippet might be helpful, please try.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let cell = ....

         cell.followButton.isHidden = !expandedIndexSet.contains(indexPath.row)
         cell.blockButton.isHidden = cell.followButton.isHidden

         return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        // Update View Model
        if(expandedIndexSet.contains(indexPath.row)){
            expandedIndexSet.remove(indexPath.row)
        } else {
            expandedIndexSet.insert(indexPath.row)
        }

        // Table View Animation
        tableView.beginUpdates()
        tableView.reloadRows(at: indexPaths, with: .automatic)
        tableView.endUpdates()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM