gpt4 book ai didi

ios - 如何在单击按钮时在 UITableView 中添加单元格?

转载 作者:行者123 更新时间:2023-11-30 12:55:34 25 4
gpt4 key购买 nike

我有一个UITableView。它有两个自定义原型(prototype)单元格。现在一个单元格已修复,它将仅在表格 View 中保留一个单元格,但另一个单元格将再次添加到表格 View 中,并在单击按钮时再次添加。

下面是 tableview 方法的代码:

func numberOfSections(in tableView: UITableView) -> Int {
return 2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr_fields.count
}

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

if indexPath.section == 0 {
let cellIndentifier="property"

let cell:CreatePropertyCell=tableView.dequeueReusableCell(withIdentifier: cellIndentifier, for: indexPath) as! CreatePropertyCell
cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.gray.cgColor
cell.txt_propertyName.attributedPlaceholder = NSAttributedString(string:"Name of property",
attributes:[NSForegroundColorAttributeName: UIColor.gray])

return cell

} else {
let cellIdentifier = "attribute"
let cell:AddProperty=tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! AddProperty
cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.gray.cgColor
cell.txt_AddProperty.attributedPlaceholder = NSAttributedString(string:"Property Attribute",
attributes:[NSForegroundColorAttributeName: UIColor.gray])
cell.btnAdd.tag=indexPath.row
return cell

}
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 15
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

let header = UIView()
header.backgroundColor = UIColor.clear
return header

}

@IBAction func actionAddProperty(_ sender: AnyObject) {
let cellIdentifier = "attribute"
let cell:AddProperty = self.tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! AddProperty
cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.gray.cgColor
cell.txt_AddProperty.attributedPlaceholder = NSAttributedString(string:"Property Attribute",
attributes:[NSForegroundColorAttributeName: UIColor.gray])
cell.tag=sender.tag!+1
arr_fields.append(cell)
tableView.reloadData()
print("tag is \(sender.tag!)")
}

现在我只想将第二个单元格添加到表格 View 中,而不是两个单元格。到目前为止,它将两个单元格添加到表格 View 中。

最佳答案

试试这个:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if section == 0 {
return 1
} else {
return arr_fields.count
}
}

插入单元格:无需重新加载表格,而是将一个单元格插入表格中:

@IBAction func actionAddProperty(_ sender: AnyObject) {

let cellIdentifier = "attribute"
let cell:AddProperty = self.tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! AddProperty
cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.gray.cgColor
cell.txt_AddProperty.attributedPlaceholder = NSAttributedString(string:"Property Attribute",
attributes:[NSForegroundColorAttributeName: UIColor.gray])
cell.tag=sender.tag!+1

let indexPath = NSIndexPath.init(forRow: arr_fields.count-1, inSection: 1)
tableView.beginUpdates()
arr_fields.append(objComment)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
tableView.endUpdates()

}

关于ios - 如何在单击按钮时在 UITableView 中添加单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40435654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com