当我调用 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)" I get the error: "Argument labels '(_:, IndexPath:)' do not match any available overloads"
最佳答案
尝试使用
let name = shopItems.keys[indexPath.row]
而不是
let name = Array(shopItems.keys) [indexPath.row]
当不是 nesesery 时,最好不要使用强力包裹。 尝试改变
let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as! UITableViewCell
到
guard let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as? UITableViewCell else {
return UITableViewCell()
}
编辑:正如@Sh_Khan所说,替换
func tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {
到
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
关于Swift 新手参数标签 '(_:, IndexPath:)' 与任何可用的重载不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57789752/