【发布时间】:2016-12-14 12:05:11
【问题描述】:
正如您在附加的 GIF 中看到的那样,UICollectionView 工作正常,但单元格中的图像不稳定(首先您看到一个图像,然后图像变为正确的图像)。
GIF 示例: http://d.pr/i/ZccO
刷新触发器:
self?.productsCollectionView.reloadData()
创建每个单元格的collectionView函数:
// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// Check if need to load more battles
if indexPath.item + Constants.bufferToLoad >= (Constants.pageSize * self.page) && Constants.pageSize * self.page < self.totalProducts {
self.page += 1
self.getProducts(false)
}
var cell:UICollectionViewCell?
if self.isLoading && indexPath.item == self.products.count-1 {
cell = collectionView.dequeueReusableCellWithReuseIdentifier(Constants.loadigCell, forIndexPath: indexPath) as? LoadingCollectionViewCell
(cell as! LoadingCollectionViewCell).activityView.startAnimating()
} else {
cell = collectionView.dequeueReusableCellWithReuseIdentifier(Constants.cellName, forIndexPath: indexPath) as? SavedProductCollectionViewCell
let productCell = cell as! SavedProductCollectionViewCell
productCell.product = self.products[indexPath.item]
}
return cell!
}
这是我为每个 CollectionViewCell 设置图像的方式:
var product:Product! {
didSet {
self.updateUI()
}
}
override func awakeFromNib() {
super.awakeFromNib()
self.layer.cornerRadius = 5
self.layer.masksToBounds = true
}
func updateUI() {
self.productImage.loadImageFromURLString(self.product.mainImage!,placeholderImage: UIImage(named: "placeHolder"))
self.productNameLabel.text = self.product?.name
self.productPriceLabel.text = self.product?.price
self.setPicks()
}
【问题讨论】:
-
您在 GIF 上看到的是可重复使用的单元格在实践中的工作方式。
标签: ios objective-c swift uicollectionview