swift - 始终将数据保存在第一个 Cell UICollectionView 中

标签 swift uicollectionview uicollectionviewcell nsindexpath

我有一个 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/

相关文章:

ios - Swift 看不到变量

ios - 无法将页脚添加到 Collection View

ios - 如何在 SwiftUI 中更改步进器的背景颜色?

ios - 如何在流程布局中有不同的单元格大小

iOS:容器 View 添加UITapGestureRecognizer拦截UICollectionView的didSelectItemAtIndexPath方法

ios - UICollectionView 在 UIScrollView 滚动问题之上

iOS UICollectionView : Cells not showing

swift - 如何更改 searchController?.searchBar 的大小和位置?

ios - 应用程序终止时的位置更新

swift - 在 iPad 上的 SwiftUI 中呈现 ActionSheet