【发布时间】:2026-02-09 23:15:02
【问题描述】:
当我调用 tableView 函数时,标题中出现错误。我的问题是为什么即使它们是请求的类型,函数也不接受这两个参数?再说一次,我是新手,所以如果答案很明显,请原谅我。
class TableViewController: UITableViewController {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as! UITableViewCell
let name = Array(shopItems.keys) [indexPath.row]
let cost = Array(shopItems.values) [indexPath.row]
cell.textLabel?.text = name + String(cost)
return cell
}
}
当我这样调用函数时:
“TableViewController.tableView(shopTableView, IndexPath: NSIndexPath)”我收到错误:“参数标签 '(_:, IndexPath:)' 不匹配任何可用的重载”
【问题讨论】:
-
你为什么要调用这个方法呢?你不应该调用这个方法。您应该编写此方法,并让框架调用它。
-
我希望在用户按下按钮时调用它,这样每次用户点击时都会添加一个单元格
-
这不是您将新单元格添加到表格视图的方式。见this。
-
如果我使用该帖子中提供的调整代码运行我的应用程序,我会收到此错误:“尝试将第 0 行插入第 1 部分,但更新后只有 1 个部分”