[英]tableview button creation dynamically in cell issue in swift
我正在尝试在tableview单元上实现动态按钮。我能够添加按钮,但是根据对最后一个单元格的要求,我不想添加按钮,但它也被添加到了那里,如下所述
这是我的cellForRowAt indexPath代码在下面的要求。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if let _ = cell {
print("cell is available")
} else {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
}
cell?.textLabel?.textAlignment = .left
cell?.textLabel?.text = list[indexPath.row]
let btn = UIButton(type: UIButtonType.custom) as UIButton
if cell?.textLabel?.text == "➕ Add Room" || list[indexPath.row] == "➕ Add Room"{
btn.isHidden = true
cell?.textLabel?.textAlignment = .center
}else{
btn.frame = CGRect(x: 146, y: 0, width: 20, height: (cell?.frame.height)!)
btn.addTarget(self, action: #selector(buttonPressed(sender: )), for: .touchUpInside)
btn.tag = indexPath.row
btn.setImage(UIImage(named: "delete.png"), for: .normal)
cellx = list[btn.tag]
cell?.contentView.addSubview(btn)
}
return cell!
}
屏幕下方显示此结果,结果如下
我不想在表格的最后一行显示的+ Add Room行中显示删除按钮。 请建议我如何如要求中所述。 我为文本(“添加会议室文本”)显示在中间并成功显示在中间创造了条件。 预先感谢,请帮助我解决此问题
第二期
在此给定列表中,如果我选择“ COPENHAGEN”并尝试将其删除,则删除上面的内容意味着BARCELONA文件被删除。 我不是从列表中删除相同的选定内容。
发生这种情况是因为UITableView重用了先前创建的单元格。 因此,对于最后一个单元格,它需要使用已创建的带有内部按钮的单元格。
最好创建自己的UITableViewCell子类并使用它。
但是,如果您不想这样做,则可以使用以下版本的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if let cell = cell {
print("cell is available")
// Remove previously created button from reused cell view
if let button = cell.contentView.subviews.first(where: { (view: UIView) -> Bool in
view.isKind(of: UIButton.self)
}) {
button.removeFromSuperview()
}
} else {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
}
cell?.textLabel?.textAlignment = .left
cell?.textLabel?.text = list[indexPath.row]
if indexPath.row == (list.count - 1) {
cell?.textLabel?.textAlignment = .center
} else {
let btn = UIButton(type: UIButtonType.custom) as UIButton
btn.frame = CGRect(x: 146, y: 0, width: 20, height: (cell?.frame.height)!)
btn.addTarget(self, action: #selector(buttonPressed(sender: )), for: .touchUpInside)
btn.tag = indexPath.row
btn.setImage(UIImage(named: "delete.png"), for: .normal)
cellx = list[btn.tag]
cell?.contentView.addSubview(btn)
}
return cell!
}
试试这个
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! cell
if indexPath.row == (list.count - 1) {
btn.isHidden = true
cell?.textLabel?.textAlignment = .center
}else{
btn.isHidden = false
btn.frame = CGRect(x: 146, y: 0, width: 20, height: (cell?.frame.height)!)
btn.addTarget(self, action: #selector(buttonPressed(sender: )), for: .touchUpInside)
btn.tag = indexPath.row
btn.setImage(UIImage(named: "delete.png"), for: .normal)
cellx = list[btn.tag]
cell?.contentView.addSubview(btn)
}
我没有测试上面的代码,只是做了一些细微的更改,也许它可以工作:)如果没有,请告诉我。
EDIT1:答案已更新
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if let _ = cell {
print("cell is available")
} else {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
}
cell?.textLabel?.textAlignment = .left
cell?.textLabel?.text = list[indexPath.row]
let btn = UIButton(type: UIButtonType.custom) as UIButton
if cell?.textLabel?.text == "➕ Add Room" || list[indexPath.row] == "➕ Add Room"{
for view in cell?.contentView.subviews as [UIView] {
if let button = view as? UIButton {
button.isHidden = true
}
}
cell?.textLabel?.textAlignment = .center
}else{
btn.frame = CGRect(x: 146, y: 0, width: 20, height: (cell?.frame.height)!)
btn.addTarget(self, action: #selector(buttonPressed(sender: )), for: .touchUpInside)
btn.tag = indexPath.row
btn.setImage(UIImage(named: "delete.png"), for: .normal)
cellx = list[btn.tag]
cell?.contentView.addSubview(btn)
}
return cell!
}
尝试这个。 这是因为您的程序流程。 将为每个表行创建btn实例,并且仅将btn的实例隐藏设置为最后一行。 表单元重用了出队单元,即使您没有将创建的btn添加到最新单元的表单元内容视图中。 因此,您需要从内容视图中搜索按钮并设置为隐藏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.