I have an issue regarding the items displayed in my UICollectionView that I am not sure how to explain, but I will as detailed as I can in the hopes that someone can help with this.
Currently, I have a UITableViewCell that contains a UICollectionView. Each UICollectionCell contains an image, retrieved from it's indexPath.
The following sample code is my implementation of the UITableViewCell containing the 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()
// 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
}
}
I am using a UIImagePickerController in my MainViewController to select an image, set it in the UICollectionViewCell, and display it in the UITableViewCell like so:
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)
}
}
Every time the user selects an image, all the images in images array is deleted and re-added in from images_ARRAY before the imagesCollectionView is reloaded. When 4 images are added, the UITableViewCell looks like this:
When the 5th image is added, the UITableViewCell dynamically changes its height, hence the self.mainVCTableView.reloadRows code, and the row is reloaded whenever the 9th, 13th, 17th, etc.. image is added to expand the height of the UITableViewCell:
The problem that I am having is when the 9th image is added, the cell is not visible on screen so when self.mainVCTableView.reloadRows is called, which ultimately calls cellForItemAt in my CustomTableViewCell, the indexPath.item is initially being reported as 4, then starts at 0, 1, 2, etc..
The following 2 screenshots is when the 8th (left) and 9th (right) image is added to the UICollectionView:
As you can see on the right, the 6th, 7th, and 8th image has disappeared and I have an empty row space where the 9th image should be.
A scroll down the table view shows:
A print out of the indexPath.item when the 9th image was added 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, once I add the 10th image, when the imagesCollectionView is reloaded, all the images re-appear again:
A print out after the 10th image has been added 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
I don't quite understand what is happening? I apologize for the long post but wanted to be specific with my problem and show illustrations so someone can have a better understanding to help me.
Thank you!





