gpt4 book ai didi

ios - UITableView 仅加载设备上可见的自定义单元格,并在添加一个单元格时崩溃

转载 作者:行者123 更新时间:2023-11-29 05:50:15 26 4
gpt4 key购买 nike

我使用带有自定义单元格的空UITableView,并且我逐一添加新项目,没有任何问题。 tableView 是可滚动的,但是当我将一个项目添加到距最后一个可见单元格多一个索引的单元格时,应用程序崩溃。

加载应用程序时,numberOfRowsinSection 为 1,并且每添加一个新条目,它就会增加 1。如果设备有 10 个可见单元格,则在 11 时崩溃。如果设备有 6 个可见单元格,则它会崩溃7. 应用程序在解包可选值时意外发现 nil。

使用标题为 UITableview Not scrolling? 的问题中的建议我在 viewDidLoad 和我的函数中尝试了以下每一行:

   self.myTableView.delegate = self

self.myTableView.autoresizingMask = UIView.AutoresizingMask.flexibleHeight;

self.myTableView.rowHeight = UITableView.automaticDimension

self.myTableView.bounces = true;

self.myTableView.reloadData()

没有任何积极的结果。

这是代码:

var enterCounts: Int = 1 

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

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextInputCell") as! TextInputTableViewCell

return cell
}


@IBAction func enter(_ sender: Any) {

let activeRow = self.enterCounts - 1
let index = IndexPath(row: activeRow, section: 0)
let cell: TextInputTableViewCell = self.myTableView.cellForRow(at: index) as! TextInputTableViewCell


if cell.myTextField.text == "" {
"DO NOTHING"
} else {

"DO STUFF"

enterCounts += 1

self.myTableView.reloadData()

let nextIndex = IndexPath(row: activeRow + 1, section: 0)


"This is the line that finds nil and crashes when row is out of view"

let nextCell: TextInputTableViewCell = self.myTableView.cellForRow(at: nextIndex) as! TextInputTableViewCell
nextCell.myTextField.text = ""
nextCell.myTextField.becomeFirstResponder()
}

}

我希望 UITableView 能够滚动并继续加载用户输入的尽可能多的单元格,就像它对第一个/可见单元格所做的那样。谢谢。

在两个答案之后,代码是:

    @IBAction func enter(_ sender: Any) {

let activeRow = self.enterCounts - 1
let index = IndexPath(row: activeRow, section: 0)
let cell: TextInputTableViewCell = self.myTableView.cellForRow(at: index) as! TextInputTableViewCell


if cell.myTextField.text == "" {
"DO NOTHING"
} else {

"DO STUFF"

enterCounts += 1
let nextIndex = IndexPath(row: activeRow + 1, section: 0)
self.myTableView.insertRows(at: [nextIndex], with: .automatic)
self.myTableView.scrollToRow(at: nextIndex,at: .middle, animated: true)

//These lines are executed only when I am in visible cells
//when a new cell is added it is not ready to become first responder it is skipped and entered text is getting mixed up.
if let nextCell: TextInputTableViewCell = self.myTableView.cellForRow(at: nextIndex) as? TextInputTableViewCell {
nextCell.myTextField.text = ""
nextCell.myTextField.becomeFirstResponder()
}
}

}

使用上面的代码,新单元格显示得非常好,但文本字段仅成为第一响应者一次,即 View 中出现的第一个单元格。

我声明我的自定义单元类如下

          @IBOutlet weak var myTextField: UITextField!

public func configure(text: String?, placeholder: String) {


myTextField.text = text
// myTextField.placeholder = placeholder

myTextField.accessibilityValue = text
// myTextField.accessibilityLabel = placeholder

}


override public func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

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

// Configure the view for the selected state
}

如果我在 tableView 之外使用 textField 并保留 tableView 仅用于显示我输入的值,事情很简单,但是当我尝试使第一响应者成为新插入单元格的 textField 时,tableView 的最后一个单元格会出现问题.

最佳答案

如果您需要添加新单元格,您可以使用此行来添加它:

let indexPath = IndexPath(row: ... , section: ...)
tableView.insertRows(at: [indexPath], with: .automatic)

之后滚动到它

   tableView.scrollToRow(at: indexPath,at: .middle, animated: true) 

最后,您可以使用此单元格

let cell = tableView.cellForRow(at: nextIndex) as! YourCustomCellClass

关于ios - UITableView 仅加载设备上可见的自定义单元格,并在添加一个单元格时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55746457/

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