gpt4 book ai didi

ios - 如何将 insertRowsAtIndexPath() 与多个自定义原型(prototype)单元格一起使用?

转载 作者:行者123 更新时间:2023-11-30 12:57:28 26 4
gpt4 key购买 nike

我的 UITableView 中有以下设置:

  • 原型(prototype)单元 1 = 始终可见,有 1 行包含 UITextField (其中的输入将使其他单元格出现/消失)和一些标签。
  • 原型(prototype)单元 2 = 根据输入显示,1 行包含来自对象的图像
  • 原型(prototype)单元 3 = 根据输入显示,1 行带有对象标签
  • 原型(prototype)单元 4 = 根据输入显示,行数基于对象中的数组

所以我有一个对象,其中包含图像(对于单元格 2 中的单行)、名称(对于单元格 3 中的单行)和数组(对于单元格 4 中的多行)。

根据原型(prototype)单元 1 中的输入,我需要填充并显示单元 2、3 和 4。这些新单元中的所有数据都来自通过在 UITextfield 中输入某些内容来决定的一个对象。从第一个单元格开始。

但我似乎无法弄清楚在哪里以及如何实现以下方法来创建新的行/单元格:

tableView?.insertRows(at:[IndexPath(row: 1, section: 0)], with: .automatic)

我需要使用它在原型(prototype)单元 2 中插入 1 行,在原型(prototype)单元 3 中插入​​ 1 行,在原型(prototype)单元 4 中插入多行。

非常感谢您的帮助。

最佳答案

我相信 inserRows() 不是最好的方法。

您可以使用cellForRowAtIndexPath,并且需要更新数据,您可以使用reloadData()reloadRowsAtIndexPaths()重新加载所有表格

>
 override func tableView(tableView: UITableView!,  
cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
if indexPath.row == 1 {
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell1 ") as YourTableViewCell
return cell
}
else if indexPath.row == 2 {
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell2 ") as YourTableViewCell
return cell
}
//do this to maintain the same cells at position 1..4 and for the rest of the rows
else {
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell5 ") as YourTableViewCell
return cell
}

}

关于ios - 如何将 insertRowsAtIndexPath() 与多个自定义原型(prototype)单元格一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40265163/

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