tableview custom cell registration fails

Viewed 173

I am trying to use a custom cell for my tableview, I have created new TableViewCell and named the cell to "Cell"

and in my viewController which has the tablView I registered the cell


        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            self.tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell")
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
        
            cell.cellTitle?.text = ads[indexPath.row].title
            
            let imageName = ads[indexPath.row].document
            let image = UIImage(named: imageName)
            cell.imgView?.image = image
            
            return cell
        }

but when I run the app it shows me the default cell in the tableView no the custom one? any advise please

whole ViewContoller code that has the table view

    import UIKit
    import Foundation
    import Alamofire
    import SwiftyJSON

    final class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
        
        var ads = [photo]()
            
        @IBOutlet private weak var tableView: UITableView!
        
        override func viewDidLoad() {
            self.tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell")
            
            super.viewDidLoad()
            downloadJson {
                self.tableView.reloadData()
            }
            
            tableView.delegate = self
            tableView.dataSource = self
        }
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 40
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return ads.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
        
            cell.cellTitle?.text = ads[indexPath.row].title
            
            let imageName = ads[indexPath.row].document
            let image = UIImage(named: imageName)
            cell.imgView?.image = image
            
            return cell
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            performSegue(withIdentifier: "SecondPageSegue", sender: self)
        }
        
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let destination = segue.destination as? SecondView {
                destination.Ads = ads[(tableView.indexPathForSelectedRow?.row)!]
            }
        }
        
        func downloadJson(completed: @escaping () ->()) {
            let url = URL(string: "https://mysite.co/apis/all.php")
            URLSession.shared.dataTask(with: url!) { (data, response, error) in
                if error == nil {
                    do {
                    self.ads = try JSONDecoder().decode([photo].self, from: data!)
                        DispatchQueue.main.async {
                            completed()
                        }
                } catch {
                    print("JSON ERROR")
                }
            
            }
            }.resume()
        }
    }

table view and Custom TableViewCell

Here is a video on how it works rn: https://imgur.com/vZEOBTm

2 Answers

This line

self.tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell")

needs to be moved out of that method, and be executed before the table view is rendered. viewDidLoad() would be a good place to move it to.

First of all you need to move self.tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell") to viewDidLoad method in your project.

Second thing TableViewCell.self should be replaced with the name of your custom cell class or if you are using xib for custom cell then please use this method to register:-

self.tableView.registerNib(UINib(nibName: "", bundle: nil), forCellReuseIdentifier: "<your custom cell xib/nib identifier>").

Also check the array "ads", do this has the data(document and title) or not, which you passed to the label and imageView of you UITableViewCell class.

Related