在自定义UIView的CellForRowAt中包含nib文件在滚动时产生抖动

问题描述:

我有一个UITableView,并且在滚动时产生抖动的tableView的“CellForRowAt”委托函数中包含我自定义UIView的nib文件。例如:在自定义UIView的CellForRowAt中包含nib文件在滚动时产生抖动

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刚刚离队像正常:

let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell 

如果您有许多不同类型的单元格,请设置多个单元格类别,但不会创建新的视图并将其添加到当前单元格

+0

不,您错了。我没有创建tableViewCell的笔尖。 customView是我放置在单元格中的视图。 let customView:CustomView = Bundle.main.loadNibNamed(“CustomView”,owner:self,options:nil)![0] as! customView //这是我的自定义视图............ cell.customViewPlacement.addSubview(customView)//这是我放置自定义视图的位置。 –

+0

这就是你错了的地方,你应该创建一个包含那个视图的单元格,而不是将它们分开,如果你无论如何都不能这么做,那么将视图添加到'CustomCell'类的' awakeFromNib',而不是'cellForRow' – Tj3n