gpt4 book ai didi

swift - 为什么我的 endUpdates 抛出错误(当它在同一个函数中工作时)

转载 作者:行者123 更新时间:2023-11-30 11:41:23 32 4
gpt4 key购买 nike

我有这个函数,它由单元中的委托(delegate)调用。

它正在调用这个数组:

var ingredientsArray = [String]()

我在 viewDidAppear() 中有这个,不确定这是否相关:

    override func viewDidAppear(_ animated: Bool) {
ingredientsTableView.beginUpdates()
ingredientsTableViewHeight.constant = ingredientsTableView.contentSize.height
ingredientsTableView.endUpdates()
}

IB网点:

@IBOutlet weak var dayPicker: UIPickerView!
@IBOutlet weak var ingredientsTableView: UITableView!
@IBOutlet weak var ingredientsTableViewHeight: NSLayoutConstraint!
@IBOutlet weak var viewHeight: NSLayoutConstraint!

表格 View :

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ingredientsArray.count + 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

if let cell = tableView.dequeueReusableCell(withIdentifier: "IngredientsCell", for: indexPath) as? IngredientsCell {
cell.delegate = self
return cell
} else {
return IngredientsCell()
}
}

这是正在调用的委托(delegate)函数:

    func textFieldBeganTapped(cell: IngredientsCell) {
let noOfRows: Int = ingredientsTableView.numberOfRows(inSection: 0)

print(noOfRows) //RETURNS 1//
print(indexOfRows) //RETURNS [0, 0]//

if noOfRows == ingredientsArray.count { //THIS SECTION WORKS//
ingredientsTableView.beginUpdates()
ingredientsTableView.insertRows(at: [IndexPath(row: noOfRows, section: 0)], with: .automatic)
ingredientsTableView.endUpdates()
ingredientsTableViewHeight.constant = ingredientsTableView.contentSize.height
}
else if noOfRows == 1 { //THIS SECTION DOESNT//
ingredientsTableView.beginUpdates()
ingredientsTableView.insertRows(at: [IndexPath(row: 1, section: 0)], with: .automatic)
ingredientsTableView.endUpdates() // CRASHES HERE //
ingredientsTableViewHeight.constant = ingredientsTableView.contentSize.height
}
}

这是它抛出的错误:

libc++abi.dylib: terminating with uncaught exception of type NSException

我没有任何未连接的 IBOutlet,并且我假设委托(delegate)在触发时已正确设置。我还发送了另一个代表(来自 textFieldDidEndEditing),但为了清楚起见,将其省略。如果需要请告诉我。我还尝试了各种 noOfRows 和索引路径值,但也不起作用。我尝试使用成分TableView.reloadData(),但这也不起作用。

我的运行理论是,我用我的逻辑做了一些非常明显愚蠢的事情,但我经常看它,我只是看不到它。或者不知何故它与 viewDidAppear 或我的 TableView 中的代码发生冲突。或者问题实际上出在该代码片段之外。

对于 x/y 问题,我尝试在单击空文本框时添加新行,但前提是前一个文本框包含文本。 (以及第一次单击第一个文本框时)

任何帮助将不胜感激。

最佳答案

因此,如果我向数组添加值,我将不再收到错误。所以我认为这与根据我的数组加载的 tableView 之间的不匹配有关。当我了解更多信息时,我会在这里更新。

编辑/更新:

所以问题是(正如下面的评论中所讨论的,据我所知)我的:ingredientsarray.count + 1 在我的 else if noOfRows == 1 中引起问题,因为 tableView 数据源(我的数组)与行数不同,因此抛出错误 - 该错误只是稍后抛出,而不是立即抛出,因此对我自己来说并不明显(因为我认为它正在工作)。

但是我以与建议略有不同的方式修复了它 - 可能是黑客工作。我没有创建 2 个甚至 3 个自定义单元格和很多委托(delegate),而是简单地将一些文本“空白”添加到 viewDidLoad 中的数组中,然后在第一次单击时添加另一个“空白”,然后每当填充文本框时,它就会替换此外,在提交到 firebase 之前,我在数组中搜索了任何剩余的“空白”文本。尚未引起任何问题,并为我节省了很多委托(delegate)/协议(protocol)。

所以这个想法:一开始有一个空白单元格,当您单击它时,它会创建另一个空白单元格(在输入文本之前总共有 2 个空白单元格),然后从那时起,它会继续创建空白单元格,但前提是您单击最后一个单元格,并且仅当您填写了前面的空白单元格时(否则您可能会继续创建数百个单元格)

代码:

    override func viewDidLoad() {
super.viewDidLoad()
ingredientsArray.append("blank")
}

func textFieldBeganTapped(cell: IngredientsCell) {
let noOfRows: Int = ingredientsTableView.numberOfRows(inSection: 0)
let indexOfRows: IndexPath = ingredientsTableView.indexPath(for: cell)!

let indexOfSecondLastRow = IndexPath(row: noOfRows - 2, section: 0)
let arrayIndexOfSecondLastRow = indexOfSecondLastRow.row

if isFirstCellEmpty == true {
isFirstCellEmpty = false
ingredientsArray.append("blank")
ingredientsTableView.beginUpdates()
ingredientsTableView.insertRows(at: [IndexPath(row: noOfRows, section: 0)], with: .automatic)
ingredientsTableView.endUpdates()
} else if isFirstCellEmpty == false {
if indexOfRows.row == noOfRows - 1 {
if ingredientsArray.indices.contains(arrayIndexOfSecondLastRow) {
if cell.ingredientsTextField.text?.isEmpty == true {
ingredientsTableView.beginUpdates()
ingredientsArray.append("blank")
ingredientsTableView.insertRows(at: [IndexPath(row: noOfRows, section: 0)], with: .automatic)
}
}
}
}
}

func textFieldEndedTapped(cell: IngredientsCell, text: String) {
if let textFieldIndexPath = self.ingredientsTableView.indexPathForRow(at: cell.center){
let arrayNumber: Int = textFieldIndexPath.row
if ingredientsArray.indices.contains(arrayNumber) {
ingredientsArray[arrayNumber] = text
} else {
ingredientsArray.insert(text, at: arrayNumber)
}
}
}

然后在使用数据之前使用它来过滤空白:

let mealIngredients = ingredientsArray.filter{$0 != "blank"}

希望我没有忘记任何事情,这对某人有帮助!

关于swift - 为什么我的 endUpdates 抛出错误(当它在同一个函数中工作时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49255100/

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