gpt4 book ai didi

ios - 检测文本字段应该从TableViewController中的Custom UITableViewCell返回以添加新行

转载 作者:行者123 更新时间:2023-12-01 19:33:13 24 4
gpt4 key购买 nike

当用户在Custom TableViewCell(包括一个TextField)中按下返回键时,我想在TableView中添加一个新行。但是,我找不到一种方法...如何在TableView中查看TextField的事件,以便添加行?

我的TableViewController

class TableViewController: UITableViewController, CustomCellDelegate,
UITextFieldDelegate {

var rowCount = 1

override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}

// MARK: - Table view data source

...

// Doesn't Do Anything
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let indexPath = IndexPath(row: rowCount-1, section: 1)
tableView.insertRows(at: [indexPath], with: .automatic)
view.endEditing(true)
return true
}

// Also does nothing
func didReturn(cell: AddActivityTableViewCell, string: String?) {
let indexPath = IndexPath(row: rowCount-1, section: 1)
tableView.insertRows(at: [indexPath], with: .automatic)
view.endEditing(true)
rowCount += 1
}

我的CustomTableViewCell
protocol CustomCellDelegate: class {
func didReturn(cell: CustomTableViewCell, string: String?)
}

class CustomTableViewCell: UITableViewCell, UITextFieldDelegate {

@IBOutlet weak var textField: UITextField!
weak var delegate: CustomCellDelegate?

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
textField.delegate = self
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

public func configureTextField(text: String?, placeholder: String) {
textField.text = text
textField.placeholder = placeholder

textField.accessibilityValue = text
textField.accessibilityLabel = placeholder
}

public func editableTextField(editable: Bool) {
if editable == true {
textField.isEnabled = true
} else {
textField.isEnabled = false
}
}

// This works
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
delegate?.didReturn(cell: self, string: textField.text)
return true
}
}


谢谢!

最佳答案

我认为您错过了牢房中的集合代表。请找到下面适合我的代码

ViewController

class TableViewController: UITableViewController, CustomCellDelegate, UITextFieldDelegate {


var rowCount = 1

override func viewDidLoad() {
super.viewDidLoad()

}

// MARK: - Table view data source

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

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rowCount
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : CustomTableViewCell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomTableViewCell
cell.textField.placeholder = "Row \(indexPath.row)"
cell.delegate = self
return cell
}

func didReturn(cell: CustomTableViewCell, string: String?) {
rowCount += 1
let indexPath = IndexPath(row: rowCount-1, section:0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
view.endEditing(true)
}


}

自定义单元
protocol CustomCellDelegate: class {
func didReturn(cell: CustomTableViewCell, string: String?)
}

class CustomTableViewCell: UITableViewCell, UITextFieldDelegate {

@IBOutlet weak var textField: UITextField!
weak var delegate: CustomCellDelegate?

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
textField.delegate = self
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

public func configureTextField(text: String?, placeholder: String) {
textField.text = text
textField.placeholder = placeholder

textField.accessibilityValue = text
textField.accessibilityLabel = placeholder
}

public func editableTextField(editable: Bool) {
if editable == true {
textField.isEnabled = true
} else {
textField.isEnabled = false
}
}

// This works
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
delegate?.didReturn(cell: self, string: textField.text)
return true
}

}

关于ios - 检测文本字段应该从TableViewController中的Custom UITableViewCell返回以添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61187095/

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