ios - 当 UIImageView.image 仍为空时如何禁用 UICollectionViewCell 的用户交互

标签 ios swift uicollectionview

是否可以在 imageView 仍为空时将 UICollectionViewCelluserInteractionEnabled 设置为 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/

相关文章:

objective-c - CATransform3D 动画的 CAKeyframeAnimation 上的平滑动画

ios - 从 View 中程序化的延续

ios - Swift/iOS - Firebase 远程配置获取值永远不会完成

swift - 快速在 UICollectionView 上添加 Youtube 缩略图

ios - 在 UICollectionReusableView 中未调用委托(delegate)

ios - 从 UICollectionViewCell 传播自定义事件

ios - Realm 过滤器 CLLocation

iphone - 我们如何从iPhone应用程序中找到我们的instagram用户ID和关注者列表以及他们的用户ID

swift - 如何将库包含到我的 Swift 项目中?

c# - Xamarin.Forms 中的 IOS 联系人邮政地址