Home > Software engineering >  Can not find indexpath in scope in tableview
Can not find indexpath in scope in tableview

Time:05-23

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.

CodePudding user response:

You are still missing one method:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     // dequeue your cell here
}

the method you use should read:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //return the number of elements to show here
}

documentation

tutorial

CodePudding user response:

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

  • Related