点击按钮时构建另一个TableViewCell


let cell = tableView.dequeueReusableCell(withIdentifier: CellButton.identifier(), for: indexPath) as! CellButton
cell.build(height: 40, color: .lighterGray())
cell.blockAction = {
let category = categories?[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: 
CellCheck.identifier(), for: indexPath) as! 
CellCheck 
}
return cell

这是我的代码,当调用blockAction(当我点击单元格中的按钮时调用函数(时,我需要构建另一个TableViewCell并删除第一个出现的

是否有任何方法可以将其加载到同一单元格内,并通过动态单元格内容的高度来扩展高度

您可以删除IndexPath,然后添加一个新的IndexPath。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!
private var buttonTapped: Bool = false
private var selectedIndexPath: IndexPath?
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "TableViewCell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if buttonTapped == true,
let olderIndexPath = selectedIndexPath,
selectedIndexPath == olderIndexPath {
let cell = UITableViewCell()
cell.backgroundColor = .red
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
cell.callback = {[weak self] in
self?.buttonTapped = true
self?.selectedIndexPath = indexPath
self?.insertNewCell(indexPath)
}
return cell
}
private func insertNewCell(_ indexPath: IndexPath) {
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .right)
tableView.insertRows(at: [indexPath], with: .left)
tableView.endUpdates()
}
}

最新更新