是否可以在 imageView
仍为空时将 UICollectionViewCell
的 userInteractionEnabled
设置为 false
或不设置让 tp performsegue
进入详细 View ?我正在尝试这样做,但没有成功。示例方法如下:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CustomCollectionViewCell
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
cell.userInteractionEnabled = false
cell.products.image = UIImage(named: "placeholderImage")
cell.activityIndicator.startAnimating()
let p = self.products[indexPath.row] as Product
if let img = UIImage(data: p.productImage) {
cell.products.image = img
}
else {
self.loadImages(p, indexPath: indexPath)
}
dispatch_async(dispatch_get_main_queue()) {
// update some UI
cell.activityIndicator.stopAnimating()
cell.userInteractionEnabled = true
}
}
return cell
}
最佳答案
我认为您错误地使用了 dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))
。您将 UI 更新放入其中(即启动旋转动画),这是错误的。我对你想要进行异步图像加载的代码的理解,以及我绝对建议使用的内容
SDWebImage library要做到这一点。这是一个很棒的库,可以实现很好的异步图像加载。
只需使用它并将所有代码放在主线程中,这样您就不需要使用任何dispatch_async。
关于ios - 当 UIImageView.image 仍为空时如何禁用 UICollectionViewCell 的用户交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34052305/