ios - 单元格洞察 UICollectionView 在可见之前不加载?

标签 ios uitableview swift uicollectionview

我有一个 UITableView,每个 table view 单元格都有 UICollectionView 洞察力。我将 UICollectionView View 用作画廊(带分页的 Collection View )。我的逻辑是这样的: 洞察方法

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // This is a dictionary with an index (for the table view row),
    // and an array with the url's of the images
    self.allImagesSlideshow[indexPath.row] = allImages

    // Calling reloadData so all the collection view cells insight
    // this table view cell start downloading there images               
    myCell.collectionView.reloadData() 
}

我调用 collectionView.reloadData() 并在

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    // This method is called from the cellForRowAtIndexPath of the Table
    // view but only once for the visible cell, not for the all cells,
    // so I cannot start downloading the images


  let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PhotoCollectionCell

    if self.allImagesSlideshow[collectionView.tag] != nil {

        var arr:[String]? = self.allImagesSlideshow[collectionView.tag]!

        if let arr = arr {

            if indexPath.item < arr.count {

                var imageName:String? = arr[indexPath.item]

                if let imageName = imageName {

                    var escapedAddress:String? = imageName.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())

                    if let escapedAddress = escapedAddress {

                        var url:NSURL? = NSURL(string: escapedAddress)

                        if let url = url {

                            cell.imageOutlet.contentMode = UIViewContentMode.ScaleAspectFill

                            cell.imageOutlet.hnk_setImageFromURL(url, placeholder: UIImage(named: "placeholderImage.png"), format: nil, failure: nil, success: nil)

                        }
                    }
                }
            }
        }
    }

    return cell
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        if self.allImagesSlideshow[collectionView.tag] != nil {

            var arr:[String]? = self.allImagesSlideshow[collectionView.tag]!

            if let arr = arr {

                 println("collection row: \(collectionView.tag), items:\(arr.count)")

                return arr.count
            }
        }

        return 0
    }

我为单元格设置了正确的图像。问题是上面的方法只为第一个 Collection View 单元格调用。因此,当用户滑动到下一个 Collection View 单元格时,会再次调用上述方法,但在下载图像时会有延迟。我希望加载所有 Collection View 单元格以洞察每个可见的 TableView 单元格,而不仅仅是第一个。

使用我发布的图像,每次都会加载“Collection View Cell(编号 0)”,但仅当用户滑动到它时才加载“Collection View Cell(编号 1)”。我如何才能强制为 Collection View 的每个单元格调用上述方法,而不仅仅是可见单元格?我想在用户滑动之前开始下载过程。

谢谢!

enter image description here

最佳答案

你是对的。函数 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 只会在单元格开始出现时调用。这是苹果的一种解决方案,称为“延迟加载”。想象一下你的表/ Collection View 有数千行,并且所有这些都同时初始化,这对内存和处理器来说都是非常糟糕的。所以苹果决定只初始化需要显示的 View 。

对于加载图片,你可以使用一些异步加载器,比如

https://github.com/rs/SDWebImage

它也很强大也很有用:D

关于ios - 单元格洞察 UICollectionView 在可见之前不加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30498741/

相关文章:

ios - Core Data 合成的是类别而不是类,为什么?

uitableview - 如何在不使用计时器的情况下快速更新 uitableview

ios - Swift:每次重新加载时都会一次又一次地添加 tableview 单元格内容?

ios - 添加了 UITableViewCell GestureRecognizer 委托(delegate)不工作

swift - CLLocationManager.location 在 10.10 中为 nil,但在 10.11 中有效

arrays - 如何一次运行多个后台线程任务?

ios - 如何在 RubyMotion 中创建字符串的 md5 散列

ios - 代码 8 : Terminating with Uncaught exception of type NSException

ios - 前台弹出通知

ios - 我可以在 Swift 中使用 actor 始终在主线程上调用函数吗?