iosswifttableviewindexpath

Can not find indexpath in scope in tableview


import UIKit

private let reuseableIdentifier = "cell"

class TableViewController: UITableViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
         
      tableView.register(UITableViewCell.self,forCellReuseIdentifier: reuseableIdentifier)
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let cell =  tableView.dequeueReusableCell(withIdentifier: reuseableIdentifier, for: indexPath ) 
        return cell
    }

    
}

So this is my code but at the dequereuseableCell for: indexPath it showing error like can not find indexPath in scope.


Solution

    1. You need to return a number greater than 0 in the method of numberOfSections and numberOfRowsInSection
    2. You need to return a cell in the method of cellForRowAt indexPath
    import UIKit
    
    private let reuseableIdentifier = "cell"
    
    class TableViewController: UITableViewController{
        
        override func viewDidLoad() {
            super.viewDidLoad()
             
          tableView.register(UITableViewCell.self,forCellReuseIdentifier: reuseableIdentifier)
        }
        override func numberOfSections(in tableView: UITableView) -> Int {
            // return number of sections
            return 1
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            // return number of rows in sections
            return 10
        }
        
        // add method
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell =  tableView.dequeueReusableCell(withIdentifier: reuseableIdentifier, for: indexPath )
            return cell
        }
    }