Swift 动态添加视图以生成表中的问题
我有这个功能:Swift 动态添加视图以生成表中的问题,swift,uitableview,tableview,Swift,Uitableview,Tableview,我有这个功能: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)! let label = UILabel(frame: CGRect(x: 100, y: 14, w
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
let label = UILabel(frame: CGRect(x: 100, y: 14, width: 400, height: 30))
label.text = "\(data[indexPath.row])"
label.tag = indexPath.row
cell.contentView.addSubview(label)
return cell
}
使用这个函数,我可以动态地添加行列表。
为什么是动态的?因为,列的数量取决于数据的类型。
不要把注意力放在那上面
该列表包含244个元素。
结果显示正常,但一旦我开始滚动,我会得到以下结果:
如何动态添加元素而不出现此错误?单元格得到重用。不断向每个单元格添加越来越多的标签 正确的解决方案是创建包含所需标签的自定义单元类。然后只需在
cellForRowAt
中设置标签的文本即可。不要在cellForRowAt
中创建和添加子视图
但请记住,您可能只想使用UITableViewCell
提供的textlab
属性。不需要自定义单元格或您自己的标签
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
cell.textLabel?.text = "\(data[indexPath.row])"
return cell
}
我找到了一个解决办法,希望这对你有用 您必须将以下代码添加到您的
cellForRowAt
中:
for item in cell.contentView.subviews
{
item.removeFromSuperview()
}
就在我拿到手机后
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
for item in cell.contentView.subviews
{
item.removeFromSuperview()
}
let label = UILabel(frame: CGRect(x: 100, y: 14, width: 300, height: 30))
label.text = "\(data[indexPath.row])"
label.tag = indexPath.row
let btn = UIButton(type: UIButtonType.custom) as UIButton
btn.backgroundColor = UIColor.red
btn.setTitle("boton", for: .normal)
btn.frame = CGRect(x:0, y:5, width: 80, height:40)
//btn.addTarget(self, action: "buttonPressed:", for: UIControlEvents.touchUpInside)
btn.tag = indexPath.row
cell.contentView.addSubview(btn)
cell.contentView.addSubview(label)
cell.tag = indexPath.row
return cell
}
UITableview在向下滚动时创建10个单元格重新创建单元格视图 你可以使用
cell.contentView.removeFromSuperview()
在cell.contentView.addSubview(label)
之前或使用cell.textlab?.text
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
cell.contentView.removeFromSuperview()
let label = UILabel(frame: CGRect(x: 100, y: 14, width: 400, height: 30))
label.text = "\(data[indexPath.row])"
label.tag = indexPath.row
cell.contentView.addSubview(label)
return cell
}
注意:此解决方案将删除单元格中的所有子视图
这不是一个好的解决方案。自定义单元格是一种更好的方法。@maddy,我知道,这只是了解swift的练习的一部分。这就是我在“不要把重点放在方法上”中所写的原因,但是是的,你是对的
custom cell
是一种更好的方法,可在向下滚动时创建10个cell重新创建cell视图。在添加子视图或使用cell.textLabel?之前,您可以使用cell.contentView.removeFromSuperview()。text如果您将其添加为答案,我会将其标记为正确的@a.masri此代码存在严重错误。此代码实际上删除了单元格的contentView
。不要那样做。真糟糕。您只想删除添加到contentView
@rmaddy的标签是的,他已经收到了,但他只是想确保这项工作的质量,定制单元格是一种更好的方法。我不理解您的评论。您发布的代码存在严重错误。这只是学习swift的练习的一部分。所以他想问一个用户:发布致命缺陷的代码如何帮助任何人学习?记住,随着时间的推移,你的答案会被其他人复制和粘贴。您只需要修复一行代码。正确地回答问题,为你的答案感到自豪。