我有一个 UICollectionView,我想始终将数据(图像)保存在第一个单元格中,而旧数据将移动到下一个单元格。因此,当我保存新照片时,它出现在第一个单元格中,我尝试在 cellForRowAtIndexPath 中使用 indexpath.row == 0 {} 但他只保存在第一个单元格中,其他单元格中什么也没有。我可以反转保存索引或给它另一种方式吗?
那么有人知道我可以在那里做什么吗?
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return picture.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("BookPicCell", forIndexPath: indexPath) as! BookPicCell
let cellcore = picture[indexPath.row]
if indexPath.row == 0 {
cell.BookImage.image = UIImage(contentsOfFile: cellcore.foto!)
}
return cell
}
感谢您的帮助。
最佳答案
您需要创建一个 UIImages 数组并将新图像附加到该数组的前面:
pictures.insert(newImage, atIndex : 0)
插入新图像后,您需要使用以下方法重新加载 UICollectionView:
self.collectionView.reloadData()
您不想对 cellForItemAtIndexPath 内的数据进行排序
关于swift - 始终将数据保存在第一个 Cell UICollectionView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36265180/