[英]indexPath.row reload in UICollectionView
我有一個3行的UICollectionViewController
,在第一個indexPath(0)中我想添加一個UIView
。 當我啟動應用程序時它可以工作,但當我進入另一個控制器並返回時, UIView
在其他行中。 這是代碼:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let customCell = collectionView.dequeueReusableCellWithReuseIdentifier(customCellId, forIndexPath: indexPath) as! CustomCell
customCell.nameLabel.text = "\(label2.text) \(materie[indexPath.row])"
customCell.setupViews()
if indexPath.row == 0 {
customCell.setupUIView()
customCell.nameLabel.text = ""
}
return customCell
}
當我啟動應用程序時,屏幕是:
當我進入另一個控制器並返回時,屏幕是:
在另一個單元格中重復方形藍色的原因是因為collectionView正在重用相同的單元格。
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let customCell = ollectionView.dequeueReusableCellWithReuseIdentifier(customCellId, forIndexPath: indexPath) as! CustomCell
// HERE YOU HAVE TO RESET THE CELL TO DEFAULT VALUES
// LIKE REMOVING THE BLUE SQUARE VIEW
customCell.view2.removeFromSuperview()
if indexPath.row == 0 {
customCell.setupUIView()
customCell.nameLabel.text = ""
} else {
customCell.nameLabel.text = "\(label2.text) \(materie[indexPath.row])"
customCell.setupViews()
}
return customCell
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.