Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift新手参数标签';(389;:,indexath:)&x27;不匹配任何可用的重载_Swift_Xcode - Fatal编程技术网

Swift新手参数标签';(389;:,indexath:)&x27;不匹配任何可用的重载

Swift新手参数标签';(389;:,indexath:)&x27;不匹配任何可用的重载,swift,xcode,Swift,Xcode,调用tableView函数时,标题中会出现错误。我的问题是,为什么函数不接受这两个参数,即使它们是请求的类型?我还是个新手,所以如果答案显而易见,请原谅我 class TableViewController: UITableViewController { func tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell { let c

调用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)”我收到错误:“参数标签'(\uxA:,IndexPath:)'与任何可用重载都不匹配”

试用

 let name = shopItems.keys[indexPath.row]
而不是

    let name = Array(shopItems.keys) [indexPath.row]
最好不要使用强力缠绕,当它不牢固时。 尝试改变

let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as! UITableViewCell

编辑:如@Sh_Khan所说,替换

func tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {


有一种简单而快速的方法可以让你自己找到合适的过载

  • 选择整个方法并按⌘/ 注释掉代码
  • 在类类型的顶层
    cellForRow
    。代码完成列表中的第一项是正确的方法
  • 按Return以插入正确的方法
  • 再次选择并按,以错误的方式进行注释⌘/.
  • 将错误方法的主体复制并粘贴到正确的主体中
  • 删除错误方法的其余部分

  • 当我更改名称和成本常量时,会出现以下错误:“无法为'Dictionary.Keys'类型的值下标'Int'类型的索引”。我还发现了我原来的错误,我不知道那是一本字典。在这种情况下,恢复你的版本是的,对不起,我应该提到这一点。基本上,我有一个字典,它接受一个字符串和一个整数,这个函数应该创建一个以字符串和整数为标签的表视图单元格。不要使用强制换行。不,在这种情况下,强制展开是受欢迎的,因为如果一切都正确连接,代码就不能崩溃。如果它这样做了,就会暴露出一个设计错误。在出现上述设计错误的情况下,可选绑定不会显示任何内容,您也不知道原因。坏的用户体验在这两方面都是一样的。@vadian-为了更好的用户体验,您可以显示带有错误信息的弹出窗口,然后关闭视图。它比crash好你为什么一开始就调用这个方法?你不应该调用这个方法。您应该编写这个方法,让框架调用它。我希望在用户按下按钮时调用它,这样每次用户点击它时都会添加一个单元格。这不是向表视图添加新单元格的方式。请参阅。如果我使用该帖子中提供的调整后的代码运行我的应用程序,我会收到以下错误:“尝试将第0行插入第1节,但更新后只有1节”谢谢,我仍然无法让它工作。Sweeper提到我不应该调用该方法,但我不明白为什么。我不需要为函数提供一个表视图来执行该方法吗?Sweeper是对的。您不能自己调用该方法。在调用
    tableView.reloadData()
    之后,框架立即调用该方法。但您还必须覆盖
    numberofrowsinssection
    以返回要显示的行数。
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {