【问题标题】:No exact matches in call to initializer in tableView在 tableView 中调用初始化程序没有完全匹配
【发布时间】:2022-01-01 16:11:53
【问题描述】:

我想学习 Swift 的组合框架,我找到了一个教程视频: https://www.youtube.com/watch?v=hbY1KTI0g70

不幸的是,我得到:

No exact matches in call to initializer 

当我尝试调用 tableView 时,定义 tableView 的行出现错误和一些其他错误,但我希望在我解决初始化此元素的问题后它们会解决。

代码:

import UIKit
import Combine

class MyCustomTableCell: UITableViewCell{ }

class ViewController: UIViewController, UITableViewDataSource {
    
    private let tableView = UITableView {
        let table = UITableView()
        table.register(MyCustomTableCell.self,
                       forceCellReuseIdentifier: "cell")
        return table
    }()

(...)

override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(tableView)
        tableView.dataSource = self
        tableView.frame = view.bounds

(...)

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return models.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MyCustomTableCell else {
            fatalError()
        }
        cellTextLabel?.text = models(indexPath.row)
        return cell
    }

整个代码长得像地狱。这就是为什么我只复制了它的关键部分(tableView 出现的地方)。 您可以在视频中看到完整的代码:

https://www.youtube.com/watch?v=hbY1KTI0g70

【问题讨论】:

    标签: ios swift xcode xcode-storyboard


    【解决方案1】:

    要么是

     private let tableView = UITableView( ... )
    

     private let tableView : UITableView = { ... }()
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-14
    • 2022-11-19
    • 2022-08-02
    • 2022-01-15
    • 1970-01-01
    • 2020-08-22
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多