首页 > 解决方案 > iOS Swift, how to delete a UIView in a TableViewCell and adjustment cell height?

问题描述

I want to add or delete some view in a TableViewCell dynamically And I want the cell Height could be adjusted to the cell content height when I delete the UIView in cell.

below is my demo, I use the

self.frame.size.height -= 100

to adjust cell height, but the result is not so good.

import UIKit
import SnapKit
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let table = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 1200))
        table.dataSource = self
        table.delegate = self
        table.estimatedRowHeight = 1
        table.rowHeight = UITableView.automaticDimension
        table.separatorColor = UIColor.blue
        table.register(CustomCell.self, forCellReuseIdentifier: "identifierid")
        view.addSubview(table)
    }

}
class CustomCell:UITableViewCell {
    let btn:UIButton = {
        let btn  = UIButton()
        btn.layer.borderColor = UIColor.black.cgColor
        btn.layer.borderWidth = 1
        btn.setTitle("delete red view", for: .normal)
        btn.setTitleColor(UIColor.black, for: .normal)
        return btn
    }()

    let v:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.red
        return v
    }()


    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.contentView.addSubview(btn)
        btn.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.size.equalTo(CGSize(width: 150,height: 50))
        }
        btn.addTarget(self, action: #selector(deleteCellImg), for: .touchUpInside)

        self.contentView.addSubview(v)
        v.snp.makeConstraints { (make) in
            make.top.equalTo(btn.snp.bottom)
            make.left.equalTo(btn)
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.bottom.equalToSuperview().offset(-10)
        }

    }

    @objc func deleteCellImg() {
        v.removeFromSuperview()
        self.frame.size.height -= 100  //100 is the red view height
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension ViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "identifierid", for: indexPath) as! CustomCell
        return cell
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}

The result is showed below

iOS simulator screenshot

When I delete a view in a cell,I don't want the other cell height would be influenced.So is there some detail I ignore?

标签: iosswift

解决方案


删除视图时调用 removeFromSuperview()。这将在您的自定义单元类中完成。

然后通过 table.reloadData() 重新加载表视图。

如果行高仍然具有误导性,请使用约束或行高。


推荐阅读