ios - 如何访问 UITableView 中每一行的相册

标签 ios swift uitableview nsindexpath

我有一个只有几行的 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

感谢您阅读本文。

最佳答案

我想在 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)
}

关于ios - 如何访问 UITableView 中每一行的相册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56046610/

相关文章:

ios - 如何在 Swift 中将 Int 转换为 NSData?

iOS >> 本地通知 >> 如何将本地通知连接到特定对象?

iphone - ABMultiValueRef 上的 iOS 地址簿错误

SwiftSoup 从evaluateJavaScript 解析

ios - 了解 dequeueReusableCellWithIdentifier

ios - 如何使用分页更改 tableview 自定义单元格中 imageview 的图像。

ios - 在 OSX 和 IOS 应用程序之间共享代码的最佳实践

ios - 我的打电话功能出了什么问题

ios - 我如何调用其他类的函数? swift ,Xcode

ios - 在 cell.content 中拖放包含自定义 UIView 的 UITableViewCell