私はUITableView
を持っています。これは2つのカスタムプロトタイプセルを持っています.1つのセルは固定されているだけで、テーブルビューには残りますが、別のセルがテーブルビューに再び追加されます&以下ボタンクリックでUITableViewでセルを追加する方法は?
は、テーブルビューのメソッドのコードです:
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!)")
}
は、今私はちょうど今のないcells.Asそれがテーブルビューに細胞の両方を追加し、両方のテーブルビューに2つ目のセルを追加したいです。
あなたはarr_fields'配列 '内の全セルを格納している理由は?どのようにこの配列を宣言しましたか?そのタイプは何ですか? –
Welll私はテキストフィールドを持っており、ボタンを追加するボタンをクリックすると、現在のcell.Soの下に新しいテキストフィールドを追加したいのですが、ボタンをクリックして新しいセルを作成し、配列に追加してデータをリロードすると思います。 – Techiee
arr_field配列のデータ型は? –