关于我的 UICollectionView
中显示的项目,我有一个问题,我不确定如何解释,但我会尽可能详细地解释,希望有人能帮助解决这个问题。
目前,我有一个包含 UICollectionView
的 UITableViewCell
。每个 UICollectionCell
都包含一个图像,从它的 indexPath
中检索。
以下示例代码是我对包含 UICollectionView
的 UITableViewCell
的实现:
extension MainViewController: UITableViewDelegate
{
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell: UITableViewCell = UITableViewCell()
....
if indexPath.section == 4
{
if let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomTableViewCell
{
if images_ARRAY.isEmpty == false
{
customCell.images_ARRAY = images_ARRAY
customCell.awakeFromNib()
}
return imagesCell
}
}
return cell
}
}
class CustomTableViewCell: UITableViewCell
{
@IBOutlet var imagesCollectionView: UICollectionView!
var images_ARRAY = [UIImage]()
var images = [INSPhotoViewable]()
override func awakeFromNib()
{
super.awakeFromNib()
// Initialization code
print("awakeFromNib")
// Through through each image of the record
for image in images_ARRAY
{
images.append(INSPhoto(image: image, thumbnailImage: SomeClass.resized(originalImage: image, withPercentage: 0.3) ) )
}
imagesCollectionView.dataSource = self
imagesCollectionView.delegate = self
}
....
override func prepareForReuse()
{
super.prepareForReuse()
print("prepareForReuse")
images.removeAll()
}
}
extension CustomTableViewCell: UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! ExampleCollectionViewCell
print("indexPath.row = \(indexPath.item)")
cell.populateWithPhoto(images[indexPath.item])
return cell
}
}
我在 MainViewController
中使用 UIImagePickerController
选择图像,将其设置在 UICollectionViewCell
中,并在 中显示>UITableViewCell
像这样:
extension MainViewController: UIImagePickerControllerDelegate
{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
// Store and display the image
if let cell = mainVCTableView.cellForRow(at: IndexPath(row: 0, section: 4) ) as? CustomTableViewCell
{
cell.images_ARRAY.append(selectedImage)
// Remove any existing images before adding all images again
cell.images.removeAll()
cell.awakeFromNib()
cell.imagesCollectionView.reloadData()
images_ARRAY = cell.images_ARRAY
if ( (images_ARRAY.count - 1) % 4) == 0 &&
images_ARRAY.count != 1
{
self.mainVCTableView.reloadRows(at: [ IndexPath(row: 0, section: 4) ], with: UITableViewRowAnimation.none)
}
}
}
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
{
picker.dismiss(animated: true, completion: nil)
}
}
每次用户选择图像时,images
数组中的所有图像都会被删除并重新添加到 imagesCollectionView
之前的 images_ARRAY
中被重新加载。添加 4 张图片后,UITableViewCell
如下所示:
当添加第 5 个图像时,UITableViewCell
会动态更改其高度,因此 self.mainVCTableView.reloadRows
代码会在第 9、13 个时重新加载该行, 17th, etc.. 添加图像以扩展 UITableViewCell
的高度:
我遇到的问题是在添加第 9 个图像时,单元格在屏幕上不可见,因此当调用 self.mainVCTableView.reloadRows
时,最终在我的 CustomTableViewCell
中调用 cellForItemAt
,indexPath.item
initially 被报告为 4,然后从 0、1、2 等开始。
以下 2 个屏幕截图是将 8th(左)和 9(右)图像添加到 UICollectionView
时的屏幕截图:
如您在右侧所见,第 6、第 7 和第 8 个图像已经消失,我在第 9 个图像应该所在的位置有一个空行空间。
向下滚动表格 View 显示:
添加第 9 图像时 indexPath.item
的打印显示如下:
indexPath.row = 4
indexPath.row = 0
indexPath.row = 1
indexPath.row = 2
indexPath.row = 3
indexPath.row = 4
indexPath.row = 5
indexPath.row = 6
indexPath.row = 7
但是,一旦我添加了 10th 图像,当重新加载 imagesCollectionView
时,所有图像再次重新出现:
添加10 图像后的打印输出显示:
indexPath.row = 0
indexPath.row = 1
indexPath.row = 2
indexPath.row = 3
indexPath.row = 4
indexPath.row = 5
indexPath.row = 6
indexPath.row = 7
indexPath.row = 8
indexPath.row = 9
我不是很明白这是怎么回事?对于这篇冗长的帖子,我深表歉意,但我想具体说明我的问题并展示插图,以便有人可以更好地理解并帮助我。
谢谢!
最佳答案
感谢 @Aravind A R 的建议,但问题最终通过在返回单元格之前在 cellForRowAt
中重新加载 UICollectionView
得到解决:
if images_ARRAY.isEmpty == false
{
customCell.images_ARRAY = images_ARRAY
customCell.awakeFromNib()
customCell.imagesCollectionView.reloadData()
}
关于ios - 当前单元格不可见时,UICollectionView 显示错误的项目数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45232799/