在tableview中找不到范围中的indexpath

发布于 2025-01-31 11:29:01 字数 796 浏览 2 评论 0 原文

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
    }

    
}

因此,这是我的代码,但在Dequereuseablecell上:IndexPath显示出错误,就像在范围中找不到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 {
        // #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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱情眠于流年 2025-02-07 11:29:01
  1. 您需要在 numberOfsections numberOfrowsInsection 的方法中返回大于0的数字
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
    }
}

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

可爱咩 2025-02-07 11:29:01

您仍然缺少一种方法:

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

您使用的方法应读:

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

documentation

tutorial

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文