Table View cell issue

I was working on the to-do list app. I added the prototype cell and its identifier as a cell. but the cells are not showing in the table view while running in the simulator. Can anyone tell me what mistakes I am making? The following is code from the view controller. import UIKit

class ViewController: UIViewController {

@IBOutlet var tableView : UITableView!

let Tasks : [String] = []
override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func didTapAdd(){
    
}

}

extension ViewController : UITextViewDelegate , UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return Tasks.count }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    
    cell.textLabel?.text = Tasks[indexPath.row]
    
    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath : IndexPath){
    tableView.deselectRow(at: indexPath, animated: true)
    
}

}

Code seems OK. But have you defined dataSource and delegate.

If you have not done it in Storyboard, you have to do it in code. In addition, Tasks is empty. Just add one to test.

class ViewController: UIViewController {
    @IBOutlet var tableView : UITableView!
    
    var tasks : [String] = []  // Declare as var, as it will change ; should start with lowercase
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tasks = ["First", "Second"]  // Just for testing
    }
    
    @IBAction func didTapAdd(){

    }
}

extension ViewController : UITextViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
        return tasks.count 
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        
        cell.textLabel?.text = tasks[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath : IndexPath){
        tableView.deselectRow(at: indexPath, animated: true)
        
    }
}

yes. I defined dataSource and delegate but I realized that when I give a defined number of cells, they show in the simulator. However since it is an empty array of tasks, the cells do not show. but in the tutorial I was watching, they also created an empty array but the cells were showing.

they also created an empty array but the cells were showing.

That would be surprising…

They probably populate the array somewhere else.

.

when I give a defined number of cells, they show in the simulator.

You risk a crash here if the number of rows does not match the count of the array

tasks[indexPath.row]
Table View cell issue
 
 
Q