Swift 新手参数标签 '(_:, IndexPath:)' 与任何可用的重载不匹配

标签 swift xcode

当我调用 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/

相关文章:

xcode - 在 Swift 中以编程方式切换标签栏

ios - 快速求和 UITableView 单元格上的值

ios - 更新购物车中的产品数量(而非数量) - Shopify Mobile Buy SDK

arrays - 如何查看图像数组中的下一张图像?

ios - 在断点条件中访问类

ios - 如何在不重新加载的情况下从 super View 中删除 View ?

swift - 如何将一个闭包附加到另一个闭包?

ios - 调用父类(super class)的 awakeFromNib

Xcode 7.1 - 找不到架构 arm64 的符号

iphone - 为什么 Xcode 5 不允许我为 iPhone 应用程序指定 iPad 图像? iTunes Artwork 图标呢?