TableView单元格选择问题

时间:2018-10-09 12:47:54

标签: ios iphone uitableview swift4

所以我在表视图中选择了一个单元格。 它会展开,显示该单元格的详细文本,然后我开始向下滚动直到该选定单元格消失。 由于重用标识符,视图中的其他单元格会自动获得所选单元格的某些属性。

有什么办法可以解决这个问题?

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

我取消选择了tableview功能

   func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPathaddres  , animated: true)
        if let cell = tableView.cellForRow(at: indexPath) as? customCell {
            cell.firstView.backgroundColor =  UIColor(red: 0.8588, green: 0.84705, blue: 0.8745, alpha: 1.0)
            print("deselected row")
        }
    }

我已经禁用了多项选择。

我附上了2张屏幕截图。 I 1是选择表格视图的第一个单元格,然后在该单元格超出范围后向下滚动https://drive.google.com/file/d/1RZlya_eVVjDzj02GKV9h0qJU8F29xMin/view?usp=drivesdk。 跳转服务器3(如该屏幕快照所示,被选中)https://drive.google.com/file/d/15k4gLUPkgB6jGZ7AWR6km0Jajst9KKxM/view?usp=drivesdk被选中

1 个答案:

答案 0 :(得分:2)

由于tableview重用了其单元格,因此如果您希望一个单元格与其他单元格不同,则需要做一些额外的检查。

  1. 通过prepareForReuse方法将单元格重置为其默认值。喜欢 隐藏您的视图并根据情况重置箭头方向。
  2. cellForRow方法中检查选定的索引,并像在didSelectRow方法中一样展开视图,如果未像在didDeselect方法中一样选择它,则将其隐藏。
相关问题