ios - 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
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?
解决方案
删除视图时调用 removeFromSuperview()。这将在您的自定义单元类中完成。
然后通过 table.reloadData() 重新加载表视图。
如果行高仍然具有误导性,请使用约束或行高。
推荐阅读
- spring - 我可以将 KafkaTemplate 与单例范围一起使用吗?
- r - 如何过滤时间窗口内最近发生的事件?
- javascript - 突出显示和禁用第一个和第二个表中的行,所选数据相同
- material-ui - 使用 Material-ui 和 ClojureScript 创建主题
- jenkins - 如何从 Jenkins REST API 获取在“构建后操作”中触发的 Jenkins 作业的详细信息?
- html - 如何使一个部分既可滚动又与页面的其余部分完全一样高?
- javascript - 订阅的自定义完整方法是否会自行取消订阅?
- python - `fitsvm() 为参数 'c' 获取了多个值` 错误
- c# - 如何在客户端启动 GMAIL Api 身份验证?
- javascript - 在 TypeScript 中简化/重构嵌套的 forEach 循环