skip to Main Content
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.

2

Answers


  1. 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

    Login or Signup to reply.
    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
        }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search