如何访问 UITableView 中每一行的照片相册

Posted

技术标签:

【中文标题】如何访问 UITableView 中每一行的照片相册【英文标题】:How to access album of photos for each row in UITableView 【发布时间】:2019-05-08 18:00:30 【问题描述】:

我有一个UITableView,只有几行。当我按住一个单元格时,相机会弹出,我可以拍照并将它们存储在相册中。 每行可以有一个相册。问题是当我点击一个相册时,每次都会打开我最后一张照片的相册,我不知道如何用indexPath解决这个问题。 这是我的代码:

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
        
    



这是我的实际问题的 GIF。在这个截图中,我只点击了照片 1 和照片 3。每次照片 2 都取我之前点击的值:

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

感谢您阅读本文。

【问题讨论】:

你就是那个说item.imagesPath?.last的人。所以现在你抱怨你看到的是最后一张图片? 那个是缩略图。对于有相册的每一行,如果相册有任何照片,我想将相册中的最后一张图片显示为缩略图。 【参考方案1】:

我猜在tapOnDefectImageView 中,您应该对单元格使用单击的 indexPath 而不是 lastIndexPath,这就是单击一行显示上次单击 indexPath 的照片的原因

所以要么在单元格内添加这个手势,要么在动作方法中添加

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

并使用

@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)

或创建

 class CustomImg:UIImageView  
   var indexPath:IndexPath? 
 

里面有这个cellForRowAt

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

然后将类分配给单元格的imageView,现在就可以了

@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)

【讨论】:

我实现了,但现在没有打开相册。看起来每次都在执行从守卫返回。 无法在单元格上保持长按手势以显示菜单?我需要切换到 TapGesture 吗? 上述方式有2个选项可以访问被点击的indexoath查看编辑...... 由于某些原因,我无法访问属性cell.defectImageView.indexPath 设置Ib中imageView的类名,并把oultet改成@IBOutlet weak var defectImageView:CustomImg!

以上是关于如何访问 UITableView 中每一行的照片相册的主要内容,如果未能解决你的问题,请参考以下文章

添加照片,自定义uitableview

asp.net 如何访问gridview控件中每一行的label控件

获取UITableView每一行的高度

swift UITableView 设置rowHeight

如何在 Java 的随机访问文件中返回一行的位置?

如何测量 C# 中每一行的执行时间