gpt4 book ai didi

ios - UICollectionView 问题中的项目数

转载 作者:行者123 更新时间:2023-11-28 15:18:13 25 4
gpt4 key购买 nike

我有一个只有 1 个部分的 UICollectionView,但它有 2 个自定义 UICollectionViewCell。单元格的主要用途是显示用户在之前的 UIViewController 中选择的媒体(主要是图像)。单元格 1 始终首先显示,并且不会改变。它用于让用户能够单击它并在 UICollectionView 中添加更多照片。另一个单元格 2 用于显示所有图像。现在,单元格的数量会因以下因素而异:

1) 用户使用相机拍照(然后

2) 用户选择了多个 PHAssets,这些 PHAssets 被转换为 UIImage 并显示在单元格中。

在这两种情况下,都会显示单元格 1。问题在于显示包含所有图像的数组中的项目数。如果我从相机胶卷中选择了 4 个 Assets ,我总是会得到 3 个图像(而不是 4 个!)。我认为这是因为我在设置 UICollectionView 时在 numberOfItemsInSection 方法中返回了 assets.count。这当然会切断数组的最后一个图像,因为单元格 1 使用它的空间。

我试过 return assets.count + 1 但是当 PHAssets 变成图像时它崩溃了,它说

Index out of range

我不知道我做错了什么。这是代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
// Add More Photos Cell
if indexPath.item == 0
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddMorePhotosCell", for: indexPath) as! AddMorePhotosCell
return cell
}
// Photos Cell
else
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoPostCVCell", for: indexPath) as! PhotoPostCVCell
if let takenImage = cameraPhotoUIImage
{
cell.cellImage.image = takenImage
}
if assets.count > 0
{
let asset = assets[indexPath.row]

var imageRequestOptions: PHImageRequestOptions
{
let options = PHImageRequestOptions()
options.version = .current
options.resizeMode = .exact
options.deliveryMode = .fastFormat
options.isSynchronous = true
return options
}
// TODO: Fix the size of the images. Right now it's 500x500
let targetSize = CGSize(width: 500, height: 500)
imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFill, options: imageRequestOptions)
{ (image, info) in
if image != nil
{
print(image!.size)
cell.cellImage.image = image
self.assetsTurnedIntoImages[indexPath.row] = image!
print("Assets turned into images is: ", self.assetsTurnedIntoImages.count)
// TODO: Fix Delete button - Right now it doesn't delete the right image from the cell, but the last one!

cell.deleteButton?.tag = indexPath.row
cell.deleteButton?.addTarget(self, action: #selector(self.deleteImage(sender:)), for: UIControlEvents.touchUpInside)
}
}
}
return cell
}
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
if assets.count > 0
{
return assets.count + 1
}
else
{
return 2
}
}

最佳答案

您需要调整数组索引。这样想:

  • 4 张图片的数组 = [A、B、C、D]
  • 第一个单元格不在数组中
  • 项目总数? 5
  • CollectionView 说“给我项目 1”——你返回 FirstCell
  • CollectionView 显示“给我第 2 项”- 您需要返回数组的第一个元素,而不是第二个元素

所以...

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
// Add More Photos Cell
if indexPath.item == 0
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddMorePhotosCell", for: indexPath) as! AddMorePhotosCell
return cell
}
// Photos Cell
else
{
// offset the index by -1 for the FirstCell
let asset = assets[indexPath.item - 1]

// the rest of the stuff
}

}

希望这是清楚的...

关于ios - UICollectionView 问题中的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46471669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com