Search code examples
swiftimagecellextension-methods

Problem using an extension to load image into a custom cell


My last question for today. I'm too newby with swift to know what's happening but my problem is as follows:

I've use the next extension to load an image from a URL and it works when I use it in a imageView in a normal view, but when the imageView is inside of a custom cell It doesn't recognize de method so I can`t use the extension. What am I doing wrong? Thank you all in advances.

My code for the extesion is: import UIKit

extension UIImageView {

func loadFrom(URLAddress: String) {
    
    guard let url = URL(string: URLAddress) else {
            return
        }
    DispatchQueue.main.async { [weak self] in
        if let imageData = try? Data(contentsOf: url) {
                if let loadedImage = UIImage(data: imageData) {
                        self?.image = loadedImage
                }
            }
        }
    }
}

And the code for the table where I try to use it into a custom cell:

  import UIKit
    
    class EventosCustomCellController: UITableViewCell {
        
        @IBOutlet weak var imEvento: UIView!
        
        @IBOutlet weak var txtNombreEvento: UILabel!
        
        @IBOutlet weak var txtFechaEvento: UILabel!
        
        @IBOutlet weak var txtEstadoEvento: UILabel!
    }
    
    class ListaEventosTableViewController: UITableViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.title = "Eventos"
            
        }
    
        // MARK: - Table view data source
    
        override func viewWillAppear(_ animated: Bool) {
            
        }
        override func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return eventos.contarEventos()
        }
    
        
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "prototipoCeldaEvento", for: indexPath) as! EventosCustomCellController
            
            let evento = eventos.buscarEventoPorID(id: indexPath.row)
            cell.txtNombreEvento?.text = evento?.nombre
            cell.txtFechaEvento?.text = evento?.fecha
            cell.txtEstadoEvento?.text = evento?.tipo
            if evento?.tipo == "deportivo"{
                cell.backgroundColor = .blue}
            else if evento?.tipo == "cultural"{
                cell.aplicarFondoDegradado()
                cell.backgroundColor = .green}
            else{
                cell.backgroundColor = .red}
            cell.layer.masksToBounds = true
               cell.layer.cornerRadius = 10
            //There is no method loadFrom when I try to use as follows
            cell.imEvento?.loadFrom(URLAddress: (evento?.imagenes![0])!)
            return cell
        }
        
    
        
        // Override to support conditional editing of the table view.
        override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            // Return false if you do not want the specified item to be editable.
            return true
        }
       }

After updating code my problem is that the cell isn`t growthn appropiately enter image description here


Solution

  • I think, you better use UIImageView instead of UIView because your extension for UIImageView and you can access to this.

    @IBOutlet weak var imEvento: UIImageView!
    

    and then it is good to override the prepareForReuse method like this:

    override func prepareForReuse() {
        super.prepareForReuse()
    
        imEvento.image = nil
    }
    

    you can read more about the prepareForReuse method from apple documentation