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

ios-tableview自定义单元格注册失败

(ios - tableview custom cell registration fails)

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

我正在尝试为表格视图使用自定义单元格,我创建了新的TableViewCell并将其命名为“ Cell”

在我的具有tablView的viewController中,我注册了单元格


        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
        }

但是当我运行该应用程序时,它向我显示了tableView中的默认单元格,没有自定义单元格?任何建议请

具有表视图的整个ViewContoller代码

    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()
        }
    }

表视图和自定义TableViewCell

这是有关其工作方式的视频rn:https : //imgur.com/vZEOBTm

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

这条线

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

需要从该方法中移出,并在呈现表视图之前执行。viewDidLoad()将是一个不错的地方。