2017-09-27 56 views
0

我有一個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 
} 

如何解決滾動問題和我的代碼中存在的問題? 感謝

回答

0
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

相關問題