表视图单元格不显示更新的数据
我达到了正确的值,并在调试会话期间打印出来。但是,当我运行该应用程序时,计算的值(newcalory)不显示特定的表格单元格文本字段。 (aka。cell.itemTotalCalory.text)您对该解决方案有任何想法吗?表视图单元格不显示更新的数据
*我附上了下面的相关代码块。
非常感谢,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! IngredientTableViewCell
cell.ingredientNameTextField.text = ingredients [indexPath.row].ingredientName
cell.numberofItem.text = "1"
let cellcalory = ingredients [indexPath.row].ingredientCalory
cell.itemTotalCalory.text = cellcalory
cell.plusButton.tag = Int(cell.itemTotalCalory.text!)! //indexPath.row
cell.plusButton.addTarget(self, action:#selector(plusAction), for: .touchUpInside)
cell.minusButton.tag = Int(cell.itemTotalCalory.text!)!
cell.minusButton.addTarget(self, action:#selector(minusAction), for: .touchUpInside)
return cell
}
@IBAction func plusAction(sender: UIButton)
{
let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell") as! IngredientTableViewCell
let buttonRow = sender.tag
if cell.numberofItem.text == "1" || cell.numberofItem.text != "1"
{
cell.numberofItem.text = "1"
let textValue1 = cell.numberofItem.text
var textValue = Int(textValue1!)
textValue = textValue! + 1
cell.numberofItem.text = String(describing: textValue)
let oldcalory = buttonRow
cell.itemTotalCalory.text = String (((textValue! * Int(oldcalory)) + Int(oldcalory)))
let newcalory = cell.itemTotalCalory.text
refresh(newcalory: newcalory!);
}
}
func refresh(newcalory :String)
{
let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell") as! IngredientTableViewCell
cell.itemTotalCalory.text = newcalory
DispatchQueue.main.async {
self.ingredientTableView.reloadData()
}
}
我找到了解决方案,下面列出的行是无用的。
let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell") as! IngredientTableViewCell
cell.itemTotalCalory.text = newcalory
我使用plusAction函数中的新值更新了配料数组,并解决了我的问题。感谢所有贴子。
你应该做的是在ingredients
阵列更新的值,然后调用ingredientTableView.reloadData()
以反映此的UI。
在refresh
方法调用dequeueReusableCell(withIdentifier:)不会按预期工作什么是你想干什么:
由于性能原因,表视图的数据源一般应该 重用的UITableViewCell对象当它分配细胞行在其 tableView(_:cellForRowAt :)方法。表视图维护数据源标记为 重用的UITableViewCell对象的队列或 列表。 当询问 为表格视图提供新的单元格时,从数据源对象调用此方法。此方法将取消现有单元格的 (如果有)或使用之前注册的 或nib文件创建新单元格。如果没有单元格可用于 重用并且您没有注册类或nib文件,则此方法 返回nil。
因此,刷新方法应该是类似于:
func refresh() {
// updating ingredients array upon reqs satisfaction...
// and then:
ingredientTableView.reloadData()
// nameOfYourRefreshControl.endRefreshing()
}
另外,如果你很确定你想从的tableView特定的细胞,你可能想使用cellForRow(at:)实例方法:
返回指定索引路径下的表格单元格。
func refresh() {
let cell = ingredientTableView?.cellForRow(at: YOUR_INDEX_PATH)
//...
}
希望这有助于。
Ahmad嗨,感谢您的回复。你对indexPath是正确的,但我很难在cellForRowAt方法中发送indexPath.row。我需要'标记'方法将卡路里发送到plusAction。当我尝试在数组中存储索引路径行时,我也遇到了错误。首先,我应该发送indexPath.row到plusAction,然后刷新函数。你有什么建议吗? – gozdebal
你能否详细说明你想达到什么目的?我所理解的是,每个单元格上都有一个按钮,并且您想根据哪个按钮被点击来编辑值,如果是这样,您需要将按钮添加到该按钮并基于它的标记,更新一个值在数组中 - 例如-... –
您需要重新加载该特定单元格以获取更新数据 –
tableview工作于可重用性的概念。当重新加载表格时创建或重用了新的单元格。不要这样做。 cell.itemTotalCalory.text = newcalory.instead this this save new calory in array or variable.and then set the value in cell.itemTotalCalory.text – commando24
我认为你对于UITableView的工作方式有些许理解。在plusAction中,您应该向您的配料数组添加一个新项目,然后只需调用该表以重新载入日期(您在刷新中执行的操作)。 tableview会知道有一个额外的项目感谢数据源,然后它会渲染额外的单元格。 –