Ios 直到有可见的单元格时,才会加载单元格insight UICollectionView?
我有一个Ios 直到有可见的单元格时,才会加载单元格insight UICollectionView?,ios,uitableview,swift,uicollectionview,Ios,Uitableview,Swift,Uicollectionview,我有一个UITableView和UICollectionView洞察每个表视图单元格。我使用UICollectionView视图作为库(带分页的集合视图)。我的逻辑是这样的: 洞察方法 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // This is a dictionary with an index (for
UITableView
和UICollectionView
洞察每个表视图单元格。我使用UICollectionView
视图作为库(带分页的集合视图)。我的逻辑是这样的:
洞察方法
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
}
func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indepath:nsindepath)->UICollectionViewCell{
//此方法是从表的cellForRowAtIndexPath调用的
//查看,但对于可见单元格仅查看一次,而不是所有单元格,
//所以我无法开始下载图像
让cell=collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,forIndexPath:indexPath)作为!PhotoCollectionCell
如果self.allImagesSlideshow[collectionView.tag]!=nil{
var arr:[String]?=self.allImagesSlideshow[collectionView.tag]!
如果让arr=arr{
如果indexath.itemInt{
如果self.allImagesSlideshow[collectionView.tag]!=nil{
var arr:[String]?=self.allImagesSlideshow[collectionView.tag]!
如果让arr=arr{
println(“集合行:\(collectionView.tag),项:\(arr.count)”)
返回arr.count
}
}
返回0
}
我为单元格设置了正确的图像。问题是,仅对第一个集合视图单元格调用上述方法。因此,当用户滑动到下一个集合视图单元格时,会再次调用上述方法,但在下载图像时会出现延迟。我希望将所有集合视图单元格加载到每个可见的表视图单元格中,而不仅仅是第一个
使用我发布的图像,每次都会加载“集合视图单元格(编号0)”,但“集合视图单元格(编号1)”仅在用户滑动时加载。如何强制对集合视图的每个单元格调用上述方法,而不仅仅是对可见的单元格?我想在用户刷卡之前启动下载过程
谢谢大家!
你说得对。函数
func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell
仅在单元格开始出现时调用。这是苹果公司的一个解决方案,叫做“延迟加载”。假设您的表/集合视图有数千行,而所有这些init同时存在,这对于内存和处理器来说都是非常糟糕的。所以苹果决定只显示需要显示的视图
对于加载映像,您可以使用一些异步加载程序,如
它功能强大,也很有用:D请添加更多代码,以便我们可以帮助您我添加了更多代码。谢谢你的建议。事实上我使用的是HanekeSwift,它可以下载并缓存图像,效果非常好。关于“延迟加载”,它确实应该是这样工作的,但是当我的表视图单元格可见时,我应该开始下载集合视图单元格的所有图像,否则当用户长时间滑动时,他会看到占位符,这是一个坏消息experience@Milen你是怎么做到的?我也有同样的问题