How to access album of photos for each row in UITableView

119 Views Asked by At

I have a UITableView with few rows. When I hold on a cell the Camera pop-up and I can take photos and to store them in an album of photos. Each row can have an album of photos. The problem is that when I click on an album, then every time will open me the album with the last picture made and I don't know how to fix this issue with the indexPath. Here is my code:

class CustomImg: UIImageView {
    var indexPath: IndexPath?
}


class ChecklistVC: UIViewController {

    lazy var itemSections: [ChecklistItemSection] = {
        return ChecklistItemSection.checklistItemSections()
    }()
    var lastIndexPath: IndexPath!
    var currentIndexPath: IndexPath! 

    ...
    ...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: Constants.checklistCell, for: indexPath) as! ChecklistCell

        let itemCategory = itemSections[indexPath.section]
        let item = itemCategory.checklistItems[indexPath.row]


        if item.imagesPath!.isEmpty{
            cell.defectImageHeightConstraint.constant = 0
        }
        else{
            let thumbnailImage = loadImageFromDiskWith(fileName: item.imagesPath?.last ?? String())
            cell.defectImageView.indexPath = indexPath
            cell.defectImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapOnDefectImageView(_:))))
            cell.defectImageHeightConstraint.constant = 100
            cell.defectImageView.isUserInteractionEnabled = true
            cell.defectImageView.image = thumbnailImage

            print("For section \(indexPath.section + 1) - row \(String(describing: indexPath.row + 1)) the album photos are: \(String(describing: item.imagesPath))")
        }
        return cell


    }

    @objc func tapOnDefectImageView(_ sender: UITapGestureRecognizer){

        guard let img = sender.view as? CustomImg, let indexPath = img.indexPath else { return }

        currentIndexPath = indexPath

        let listImagesDefectVC = storyboard?.instantiateViewController(withIdentifier: "ListImagesDefectID") as! ListImagesDefectVC
        let item = itemSections[indexPath.section].checklistItems[indexPath.row]

        listImagesDefectVC.listImagesPath = item.imagesPath
        listImagesDefectVC.isPhotoAccessedFromChecklist = true
        listImagesDefectVC.delegate = self
        navigationController?.pushViewController(listImagesDefectVC, animated: true)
    }


    // A menu from where the user can choose to take pictures for "Vehicle Damage/Defects" or "Trailer Damage/Defects"
    func showOptionsForAddPhoto(_ indexPath: IndexPath){

        let addPhotoForVehicle = UIAlertAction(title: "Add photo for Vehicle", style: .default) { action in
            self.lastIndexPath = indexPath // Get the position of the cell where to add the vehicle photo
            self.showCamera(imagePicker: self.imagePicker)
        }
        let addPhotoForTrailer = UIAlertAction(title: "Add photo for Trailer", style: .default) { action in
            self.lastIndexPath = indexPath
            self.showCamera(imagePicker: self.imagePicker)
        }
        let actionSheet = configureActionSheet()
        actionSheet.addAction(addPhotoForVehicle)
        actionSheet.addAction(addPhotoForTrailer)
        self.present(actionSheet, animated: true, completion: nil)
    }


    // Get the list of the images from ListImagesDefectVC
    extension ChecklistVC: ListImagesDefectDelegate {

        func receiveListImagesUpdated(imagesFromList: [String]?) {

            print("Received Array: \(imagesFromList ?? [])")

            let item = itemSections[currentIndexPath.section].checklistItems[currentIndexPath.row]
            item.imagesPath = imagesFromList
        }
    }
}


Here is a GIF with my actual issue. In this capture I click only on Photo 1 and Photo 3. And every time Photo 2 take the value of what I clicked before:

http://g.recordit.co/VMeGZbf7TF.gif

Thank you if you are reading this.

1

There are 1 best solutions below

10
Shehata Gamal On BEST ANSWER

I guess in tapOnDefectImageView you should use the clicked indexPath for the cell not lastIndexPath which is the reason why clicking a row shows photos of last clicked indexPath

so either add this gesture inside the cell and in the action method do

delegate?.tapOnDefectImageView(self) //// self = cell

and use

@objc func tapOnDefectImageView(_ gest:ChecklistCell){
    guard let indexPath = tableView.indexPath(cell) else { return }
    let listImagesDefectVC = storyboard?.instantiateViewController(withIdentifier: "ListImagesDefectID") as! ListImagesDefectVC
    let item = itemSections[indexPath.section].checklistItems[indexPath.row]

    listImagesDefectVC.listImagesPath = item.imagesPath
    listImagesDefectVC.isPhotoAccessedFromChecklist = true
    listImagesDefectVC.delegate = self
    navigationController?.pushViewController(listImagesDefectVC, animated: true)
}

or create

 class CustomImg:UIImageView { 
   var indexPath:IndexPath? 
 }

with this inside cellForRowAt

  cell.defectImageView.indexPath = indexPath 
  cell.defectImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapOnDefectImageView)))

then assign the class to the imageView of the cell and now you can do

@objc func tapOnDefectImageView(_ sender:UITapGestureRecognizer){
    guard let img = sender.view as? CustomImg ,  let indexPath = img.indexPath  else { return }
    let listImagesDefectVC = storyboard?.instantiateViewController(withIdentifier: "ListImagesDefectID") as! ListImagesDefectVC
    let item = itemSections[indexPath.section].checklistItems[indexPath.row]

    listImagesDefectVC.listImagesPath = item.imagesPath
    listImagesDefectVC.isPhotoAccessedFromChecklist = true
    listImagesDefectVC.delegate = self
    navigationController?.pushViewController(listImagesDefectVC, animated: true)
}