从UitableViewCell中删除所有子图层

问题描述 投票:0回答:4

我有UITableView有2个扩展的部分。因此,当我点击部分的标题时,行将被隐藏:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    print("Section: \(indexPath.section), Row \(indexPath.row), \(sections[indexPath.section].expanded)")

    if sections[indexPath.section].expanded {
        return 88
    } else {
        return 0
    }
}

我正在向UITableView中的单元格添加UIlabel,如下所示:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

    let label = UILabel()
    cell.contentView.addSubview(label)
    label.translatesAutoresizingMaskIntoConstraints = false

    label.leftAnchor.constraint(equalTo: cell.leftAnchor).isActive = true
    label.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
    label.widthAnchor.constraint(equalTo: cell.widthAnchor).isActive = true
    label.heightAnchor.constraint(equalToConstant: 30).isActive = true

    label.text = "row = \(indexPath.row)"

    return cell
}

上面的工作正常,但单元格没有正确显示屏幕,row1的内容打印在另一行。

如果我添加下面的代码,当我点击任何部分的标题时,行将在tableView上正确显示,但只是一次,我的意思是如果我再次点击标题,CellForRowAt内部会发生错误function(线程1:EXC_BAD_ACCESS(code = EXC_I386_GPFLT)):

cell.contentView.layer.sublayers?.forEach { $0.removeFromSuperlayer() }

有什么建议?

编辑:只是解释如何扩展部分,我将添加以下内容:

protocol ExpandableHeaderViewDelegate {
    func toggleSection(header: ExpandableHeaderView, section: Int)
}

class ExpandableHeaderView: UITableViewHeaderFooterView {

    var delegate: ExpandableHeaderViewDelegate?
    var section: Int!

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)

        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectHeaderAction)))
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func selectHeaderAction(gestureRecognizer: UITapGestureRecognizer) {
        let cell = gestureRecognizer.view as! ExpandableHeaderView
        delegate?.toggleSection(header: self, section: cell.section)
    }

    func toggleSection(header: ExpandableHeaderView, section: Int) {
        tableView.beginUpdates()
        tableView.endUpdates()
    }

}
swift uitableview layer
4个回答
0
投票

如果问题仅仅是细胞数据不匹配,那么尝试使用

override func prepareForReuse() {
    super.prepareForReuse()
    self.label.text = ""
}

使用null值声明单元格类中的UILabel,而不是在cellForRowAt中声明它


0
投票

在cellForRowAt中尝试这个:

if tableView.rectForRow(at: indexPath).height == 0.0 {
            return UITableViewCell()
}

它应该高于所有其他代码。


0
投票

尝试(在添加新标签之前调用它)

cell.contentView.subviews.removeAll() 

代替

cell.contentView.layer.sublayers?.forEach { $0.removeFromSuperlayer() } 

0
投票

我的问题由[Knight0fDragon]解决了。

    extension UIView
{
    func clearSubviews()
    {
        for subview in self.subviews as! [UIView] {
            subview.removeFromSuperview();
        }
    }
}

然后

        cell.contentView.clearSubviews()

感谢所有善良的人们的支持。

© www.soinside.com 2019 - 2024. All rights reserved.