iosswiftxcodexcode-storyboard

No exact matches in call to initializer in tableView


I want to learn Combine framework for Swift and I have found a tutorial video: https://www.youtube.com/watch?v=hbY1KTI0g70

Unfortunately, I get:

No exact matches in call to initializer 

error on the line which defines tableView and some other errors when I try to call the tableView, but I hope they will resolve after I fix the issue with initialising this element.

The code:

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
    }

The whole code is long as hell. That is why I copied only the crucial parts of it (where the tableView occurs). You can see the full code in the video:

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


Solution

  • It's either

     private let tableView = UITableView( ... )
    

    or

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