Swift-UICollectionView重复(复制)单元格
嗨,我有一个从flickr拍摄的100张图片的数组。当我使用UICollectionView时,我显示100个单元格,屏幕上只有8个单元格,当我向下滚动查看下一个8个单元格时,它们与上一个和执行时相同Swift-UICollectionView重复(复制)单元格,swift,duplicates,uicollectionview,cells,Swift,Duplicates,Uicollectionview,Cells,嗨,我有一个从flickr拍摄的100张图片的数组。当我使用UICollectionView时,我显示100个单元格,屏幕上只有8个单元格,当我向下滚动查看下一个8个单元格时,它们与上一个和执行时相同 func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! println(“某物”)在控制台
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
println(“某物”)在控制台中只写了8次某物,但数组的内容是100
我用这个:
func collectionView(collectionView: UICollectionView?, numberOfItemsInSection section: Int) -> Int {
return self.photosUrls.count
}
它产生100个细胞,但细胞总是重复。。。谁知道如何在新版XCode GM seed中击败Swift
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
let cell: ImageCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as ImageCell
let imgUrl = photosUrls[indexPath!.row]
let api_url = NSURL.URLWithString(imgUrl)
let URLReq = NSURLRequest(URL: api_url)
let NSOpQ:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(URLReq, queue: NSOpQ, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if(error == nil){
cell.theWPImage!.image = UIImage(data: data)
}
})
return cell
}
我想您不明白
UITableView
和UICollectionView
委托方法是如何工作的
iOS不会同时加载100个单元,因为它会非常慢,而且不是一个好的用户体验。因此,只有当单元格出现在屏幕上时,才会调用方法cellForItemAtIndexPath
或cellforrowtIndexPath
无论何时向下滚动,都会看到再次调用方法cellForItemAtIndexPath
来生成新单元格。如果将代码println(indexPath.row)
放在cellForItemAtIndexPath
中,则在上下滚动时,实际上会看到单元格编号被重复打印
如果在向下滚动后看到相同的单元格,可能是由于您在cellForItemAtIndexPath
上的代码所致。除非您粘贴上面的完整源代码,否则我无法帮助您。或者它可能是XCode-GM-seed的UICollectionView
中的一个bug
在XCode 6 Beta 7中尝试相同的项目并报告如何?我对XCode GM中的UICollectionView也有问题。它与约束有关。请看:我现在使用的是XCode 6 Beta 7 您的问题是试图在cellForRow函数中加载图像。下载图像时,您的collectionView将显示另一个单元格。 正确的方法之一是将viewDidLoad中的图像加载到数组中,然后从cellForRow中的该数组中加载