获取 tableview 单元格按钮以添加新的 tableview 单元格
get tableview cell button to add a new tableview cell
我希望我的 swift 代码在每次按下按钮时添加一个新的表格视图单元格。你可以在下面的 gif 中看到我正在寻找的东西。此代码应在 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 中添加按钮。每次按下 tableview 单元格按钮时,都会出现一个带有按钮的新单元格。
enter image description here
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private let myArray: NSArray = ["First"]
var myTableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
myTableView.dataSource = self
myTableView.delegate = self
self.view.addSubview(myTableView)
myTableView.translatesAutoresizingMaskIntoConstraints = false
myTableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")
NSLayoutConstraint.activate([
myTableView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.90),
myTableView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
myTableView.topAnchor.constraint(equalTo: view.topAnchor),
myTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
])
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Num: \(indexPath.row)")
print("Value: \(myArray[indexPath.row])")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
cell.buttonTapCallback = {
}
return cell
}
}
class MyCell: UITableViewCell {
var buttonTapCallback: () -> () = { }
let button: UIButton = {
let btn = UIButton()
btn.setTitle("Button", for: .normal)
btn.backgroundColor = .systemPink
btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
return btn
}()
let label: UILabel = {
let lbl = UILabel()
lbl.font = UIFont.systemFont(ofSize: 16)
lbl.textColor = .systemPink
return lbl
}()
@objc func didTapButton() {
buttonTapCallback()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//Add button
contentView.addSubview(button)
button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
//Set constraints as per your requirements
button.translatesAutoresizingMaskIntoConstraints = false
button.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20).isActive = true
button.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).isActive = true
button.widthAnchor.constraint(equalToConstant: 100).isActive = true
button.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
您可能错过了实际添加要显示的新项目
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
cell.buttonTapCallback = { [weak self] in
guard let self = self else { return }
self.myArray.append("NewItem")
self.myTableView.reloadData()
}
return cell
}
在 tableView(_: cellForRowAt:)
中,您需要使用 MyCell's
init(style:reuseIdentifier:)
而不是 dequeueReusableCell(withIdentifier:for:)
创建单元格,即
let cell = MyCell(style: .default, reuseIdentifier: "MyCell")
接下来,在buttonTapCallback
closure
中你需要添加一个新元素到myArray
并在myTableView
上调用reloadData()
,即
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = MyCell(style: .default, reuseIdentifier: "MyCell")
cell.buttonTapCallback = {[weak self] in
self?.myArray.append("NewCell-\(indexPath.row)")
self?.myTableView.reloadData()
}
return cell
}
注意: 使用 Swift[=34 时使用 Swift 类型=] 而不是 Objective-C 类型。在您的代码中使用 [String]
而不是 NSArray
。
我希望我的 swift 代码在每次按下按钮时添加一个新的表格视图单元格。你可以在下面的 gif 中看到我正在寻找的东西。此代码应在 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 中添加按钮。每次按下 tableview 单元格按钮时,都会出现一个带有按钮的新单元格。
enter image description here
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private let myArray: NSArray = ["First"]
var myTableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
myTableView.dataSource = self
myTableView.delegate = self
self.view.addSubview(myTableView)
myTableView.translatesAutoresizingMaskIntoConstraints = false
myTableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")
NSLayoutConstraint.activate([
myTableView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.90),
myTableView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
myTableView.topAnchor.constraint(equalTo: view.topAnchor),
myTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
])
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Num: \(indexPath.row)")
print("Value: \(myArray[indexPath.row])")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
cell.buttonTapCallback = {
}
return cell
}
}
class MyCell: UITableViewCell {
var buttonTapCallback: () -> () = { }
let button: UIButton = {
let btn = UIButton()
btn.setTitle("Button", for: .normal)
btn.backgroundColor = .systemPink
btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
return btn
}()
let label: UILabel = {
let lbl = UILabel()
lbl.font = UIFont.systemFont(ofSize: 16)
lbl.textColor = .systemPink
return lbl
}()
@objc func didTapButton() {
buttonTapCallback()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//Add button
contentView.addSubview(button)
button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
//Set constraints as per your requirements
button.translatesAutoresizingMaskIntoConstraints = false
button.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20).isActive = true
button.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).isActive = true
button.widthAnchor.constraint(equalToConstant: 100).isActive = true
button.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
您可能错过了实际添加要显示的新项目
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
cell.buttonTapCallback = { [weak self] in
guard let self = self else { return }
self.myArray.append("NewItem")
self.myTableView.reloadData()
}
return cell
}
在 tableView(_: cellForRowAt:)
中,您需要使用 MyCell's
init(style:reuseIdentifier:)
而不是 dequeueReusableCell(withIdentifier:for:)
创建单元格,即
let cell = MyCell(style: .default, reuseIdentifier: "MyCell")
接下来,在buttonTapCallback
closure
中你需要添加一个新元素到myArray
并在myTableView
上调用reloadData()
,即
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = MyCell(style: .default, reuseIdentifier: "MyCell")
cell.buttonTapCallback = {[weak self] in
self?.myArray.append("NewCell-\(indexPath.row)")
self?.myTableView.reloadData()
}
return cell
}
注意: 使用 Swift[=34 时使用 Swift 类型=] 而不是 Objective-C 类型。在您的代码中使用 [String]
而不是 NSArray
。