索引路径在调用后没有放置图片

问题描述

我的Swift代码目标是当用户从相册导入照片时,将图像放置在customtv类的imageview图片中。如果您按两次导入按钮,则该代码有效,但是第一次,该代码不起作用,什么也没有发生,您可以在下面的gif中看到我测试该代码。我试图将索引从1开始,但是不起作用。我认为imagePickerController是引起问题的原因。

enter image description here

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate & UINavigationControllerDelegate {
    
     var selectedindexPath = IndexPath(row: 0,section: 0)
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 118
    }
    @objc func importPhoto(_ button: UIButton) {
           selectedindexPath = IndexPath(row: button.tag,section: 0)
           let imagePicker = UIImagePickerController()
           imagePicker.delegate = self
           imagePicker.sourceType = .photoLibrary
           present(imagePicker,animated: true,completion: nil)
       }
       
    func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let selectedImage = info[.originalImage] as? UIImage else {
            fatalError("Expected a dictionary containing an image,but was provided the following: \(info)")
        }
        guard let cell = tableview.cellForRow(at: selectedindexPath) as? customtv else { return }
        cell.pic.image = selectedImage
        
        tableview.reloadRows(at: [selectedindexPath],with: .automatic)
        dismiss(animated: true,completion: nil)
    }
       
       @objc func image(_ image: UIImage,didFinishSavingWithError error: Error?,contextInfo: UnsafeRawPointer) {
           if let error = error {
               // we got back an error!
               let ac = UIAlertController(title: "Save error",message: error.localizedDescription,preferredStyle: .alert)
               ac.addAction(UIAlertAction(title: "OK",style: .default))
               present(ac,animated: true)
           } else {
               let ac = UIAlertController(title: "Saved!",message: "Your altered image has been saved to your photos.",animated: true)
           }
       }
      
    
    var tableview = UITableView()
    
     
      


      func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! customtv
            cell.btn.tag = indexPath.row
            cell.btn.addTarget(self,action: #selector(importPhoto),for: .touchDown)
    
            
         
            return cell
        }
    
      


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableview.frame = CGRect(x: 0,y: 0,width: view.frame.width,height: view.frame.height)
        view.addSubview(tableview)
        tableview.register(customtv.self,forCellReuseIdentifier: "cell")
        tableview.delegate = self
        tableview.dataSource = self
        
        
    }
    


}


class customtv: UITableViewCell {
    lazy var backView : UIView = {
        let view = UIView(frame: CGRect(x: 10,y: 6,width: self.frame.width,height: 110))
        view.backgroundColor = .green
        print(self.frame.width)
        return view
    }()
    
    
    
    override func layoutSubviews() {

        backView.frame =  CGRect(x: 0,width: bounds.maxX,height: 110)
        
        
    }

    lazy var btn : UIButton = {
        let press = UIButton(frame: CGRect(x: 180,y: 40,width: 120,height: 50))
        press.backgroundColor = .blue
        press.setTitle("Import",for: .normal)
        
        
        return press
    }()
    
    lazy var pic : UIImageView = {
        let press = UIImageView(frame: CGRect(x: 20,height: 50))
        press.backgroundColor = .red
   
        
        
        return press
    }()
  
    
    override func setSelected(_ selected: Bool,animated: Bool) {
        super.setSelected(animated,animated: true)
        addSubview(backView)
        backView.addSubview(pic)
        backView.addSubview(btn)
    }
    
    
    
}

解决方法

您以错误的方式使用了表格视图。表格视图使用UITableViewDataSource方法通过调用tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath)来检索数据。在该函数中-仅在那里-您可以用适当的数据填充单元格。 在imagePickerController(_:,didFinishPickingMediaWithInfo:)中设置图片时,会发生 下一次用户滚动或表视图确定需要重新显示单元格时,它将调用cellForRowAt,然后重置您刚刚设置的图像。

因此,您需要保留对用户在属性中选择的图像的引用

class ViewController {
    var selectedImages = [IndexPath:UIImage]()
    var selectedRow:Int? 

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

        // set the image if there is one,otherwise set to nil
        cell.imageView?.image = selectedImages[indexPath]
    }
    
    @objc func importPhoto(_ button: UIButton) {
        selectedRow = button.tag
        // ...
    }

    func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let selectedImage = info[.originalImage] as? UIImage else {
            fatalError("Expected a dictionary containing an image,but was provided the following: \(info)")
        }
        guard let selectedRow = self.selectedRow else {
            fatalError("selectedRow is nil,which is unexpected")
        }
        let selectedIndexPath = IndexPath(row:selectedRow,section:0)
        selectedImages[selectedIndexPath] = selectedImage
        
        tableview.reloadRows(at: [selectedIndexPath],with: .automatic)
        dismiss(animated: true,completion: nil)
    }
    
}

除了将(二进制图像)数据存储在selectedImages中之外,您还可以仅存储对相机胶卷的引用。 有关详情,请参见Storing a reference to a camera roll image - Swift 4

,

删除下面的行

tableview.reloadRows(at: [selectedIndexPath],with: .automatic)

从方法func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {