首页 > 解决方案 > TableView didSelect 行问题

问题描述

所以问题是当一个单元格被点击时,显示所需的数据,当再次点击同一个单元格时(再次显示所需的数据。)

But when one cell is selected and we again select other cell (then the data is been shown of second tapped cell but the first one is not deselected).

我该如何处理这个问题?

var selectedIndex = -1

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    UIView.animate(withDuration: 1) {
        self.labelViewHeightConstraint.constant = 60
        self.labelLeadingConstraint.constant = 136
        self.view.layoutIfNeeded()
    }


    let cell = tableView.cellForRow(at: indexPath) as! CustomCell
    if(selectedIndex == indexPath.row) {
        selectedIndex = -1
            print("deselect")
        UIView.animate(withDuration: 0.4) {

            cell.secondView.isHidden = true
            cell.firstView.backgroundColor =  UIColor(red: 0.8588, green: 0.84705, blue: 0.8745, alpha: 1.0)
        }
    } else {
        cell.secondView.isHidden = false
    }
        self.expandTableView.beginUpdates()
        //self.expandTableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic )
        self.expandTableView.endUpdates()
    }

标签: iosswiftuitableview

解决方案


您可以通过设置 tableView 属性来存档单个选择,例如 belwo

tableView.allowsMultipleSelection = false

这也可以从 Attributes Inspector 完成 在此处输入图像描述

希望这可以帮助


推荐阅读