Warm tip: This article is reproduced from serverfault.com, please click

tableview custom cell registration fails

发布于 2020-12-02 08:15:29

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

Questioner
user2989513
Viewed
0
Gereon 2020-12-02 16:36:45

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.