我有一个UITATIONVIEW,我在tableview的" cellforrowat"代表函数中包含一个自定义uiview的笔尖文件,该函数在滚动时会产生混乱。例如:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"cell", for: indexPath) as! CustomCell
let customView: CustomView = Bundle.main.loadNibNamed("CustomView", owner:
self, options: nil)![0] as! customView
cell.customViewPlacement.addSubview(customView)
return cell
}
如何修复滚动,我的代码中有什么问题?谢谢
cell.customViewPlacement.addSubview(customView)
这不是您在cellForRow
中创建单元格和重复使用的方式,而是您基本上创建新视图并在每次单元格滚动时添加(继续重复),从而使其发抖,您必须使用该视图创建单元格,并在viewDidLoad
中注册您的笔尖文件,例如:
tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
和在 cellForRow
中像普通一样的dequeue:
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
如果您有许多不同类型的单元格,请进行多个单元格,而不是新视图并添加到当前的单元格