gpt4 book ai didi

ios - cellforRowat : indexPath not returning correct value for text field

转载 作者:行者123 更新时间:2023-11-29 05:31:43 25 4
gpt4 key购买 nike

用户调整后无法从 tableView 中的自定义单元格中提取新值。

我有一个自定义单元格(属于 RoutineRowCell 类),其中包含 2 个按钮和一个包含“整数”值的文本字段。每次按下按钮时,文本字段中的值分别增加和减少 1。我已在 ViewController 中注册了该值,加载时该值将由默认值填充并显示在 tableView 中。这一切都可以正常工作。

但是,一旦用户通过按下按钮完成了对该数字的调整,我将尝试使用 cellforRowAt: Indexpath 返回单元格,然后提取新值并将其添加回数据源数组。

单元格似乎已正确返回,但它没有从返回的单元格中找到新值,因此数据源未更新我做错了什么?

@IBAction func addRowButtonPressed(_ sender: UIBarButtonItem) {

//Extend the datasourceArray with new item containing default value.

myRoutine.routineStepArray.append(myRoutine.routineStep1)

//update any changed values in the existing UITableView back to the class containing the datasourceArray i.e. myRoutine.routineStepArray.

for row in 0...myRoutine.routineStepArray.count - 1 {



if let cell = self.routineTable.dataSource?.tableView(self.routineTable, cellForRowAt: IndexPath(row: row, section: 0)) as? RoutineRowCell {
let newBeatsInBar = cell.beatsInBar.text

myRoutine.routineStepArray[row] = RoutineStep(beats: cell.beatsInBar.text!, note: cell.noteValue.text!, bars: cell.numberOfBars.text!)


} else {

fatalError("No cell found at [\(row), 0]")
}
}


routineTable.reloadData()
}

预期结果是 beatsInbar(属于 RoutineRowCell 类)的新值被保存回数据源数组(myRoutine.RoutineStepArray),添加了一个新行,并重新加载了完整的表格,其中显示了现有单元格的调整值以及显示了默认值的新单元格。

实际发生的情况是 newBeatsInBar 包含旧的默认值,因此当表重新加载时,调整后的值会丢失。我做错了什么?

最佳答案

尝试换行

`if let cell = self.routineTable.dataSource?.tableView(self.routineTable, cellForRowAt: IndexPath(row: row, section: 0)) as? RoutineRowCell`

if let cell = tableView(self.routineTable, cellForRowAt: IndexPath(row: row, section: 0)) as? RoutineRowCell

编辑

或者,当单击相应行中的增加和减少按钮时,您可以更新数据数组中的值。为了识别按钮,您可以将按钮的标签设置为indexPath.row。只需使用标签值来更新数据源中相应索引中的数据。

关于ios - cellforRowat : indexPath not returning correct value for text field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57473842/

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