gpt4 book ai didi

ios - TableViewCell 中的 Swift 3.0 文本字段未显示

转载 作者:行者123 更新时间:2023-11-28 12:18:14 24 4
gpt4 key购买 nike

我正在尝试使用自定义单元格在表格 View 上显示文本字段。我想我已经正确设置了委托(delegate)协议(protocol),但是加载时,tableview 不显示文本字段。我在自定义单元格中添加了一个文本字段,将类设置为 CustomCell,并将标识符更改为“Cell”,但我不确定还缺少什么。

带 TableView 的 View Controller :

@IBOutlet weak var tableView: UITableView! 


override func viewDidLoad() {
super.viewDidLoad()

tableView.delegate = self
tableView.dataSource = self
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Hello")

}

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

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

return data.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
cell.cellDelegate = self
cell.contentView.bringSubview(toFront: cell.textField)
cell.textField.delegate = self as? UITextFieldDelegate
cell.textField.text = "Test"

return cell
}

func didPressTextField(indexPath: IndexPath) {
print("textfield was tapped")
}

自定义单元VC:

protocol CustomCellDelegate : class {
func didPressTextField(indexPath: IndexPath)
}

class CustomCell: UITableViewCell {

weak var cellDelegate: CustomCellDelegate?
var indexPath: IndexPath!

override func awakeFromNib() {
super.awakeFromNib()
self.cellDelegate = nil
// Initialization code
}

@IBOutlet weak var textField: UITextField!
@IBAction func textFieldWasTapped(_ sender: UITextField) {
self.cellDelegate?.didPressTextField(indexPath: indexPath)
}

最佳答案

您的数据源是空的。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// data is not containing any value
return data.count
}

关于ios - TableViewCell 中的 Swift 3.0 文本字段未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45704924/

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