I have a problem with the elements displayed in mine UICollectionView, which I'm not sure how to explain, but I will be as detailed as possible in the hope that someone can help with this.
I currently have UITableViewCellone that contains UICollectionView. Each UICollectionCellcontains an image extracted from it indexPath.
The following code example is my implementation UITableViewCell, containing UICollectionView:
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()
print("awakeFromNib")
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
}
}
I use UIImagePickerControllerin my MainViewControllerway to select an image, set it to UICollectionViewCelland display it in UITableViewCelllike this:
extension MainViewController: UIImagePickerControllerDelegate
{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
if let cell = mainVCTableView.cellForRow(at: IndexPath(row: 0, section: 4) ) as? CustomTableViewCell
{
cell.images_ARRAY.append(selectedImage)
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 images_ARRAY imagesCollectionView. 4 , UITableViewCell :

5- UITableViewCell , , self.mainVCTableView.reloadRows, , 9, 13, 17 . UITableViewCell:

, 9- , , , self.mainVCTableView.reloadRows, cellForItemAt CustomTableViewCell, indexPath.item 4, 0, 1, 2 ..
The following 2 screenshots when in UICollectionView:
Added image 8th (left) and 9th (right)

As you can see on the right, the 6th, 7th and 8th images have disappeared, and I have an empty row space where the 9th image should be.
Scrolling down the table view shows:

The printout indexPath.itemwhen adding a 9th image shows the following:
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
However, as soon as I add the 10th image when it reboots imagesCollectionView, all the images reappear again:

The listing after adding the 10th image shows:
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
, ? , , - , .
!