繁体   English   中英

TableView单元格选择问题

[英]TableView cell selection issue

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

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

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已选中

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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