【问题标题】:What am i doing in the code below that is causing me to get errors?我在下面的代码中做了什么导致我出错?
【发布时间】:2017-11-07 09:40:51
【问题描述】:

我收到以下错误:

1) 用于检查可选项的“UITableViewCell”类型的非可选表达式

2) 'UITableViewCell' 类型的值没有成员 'configureCell' 请

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

        if let cell:UITableViewCell = countryList.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell // Error 1 happens here {

            let text: String!

            if inSearchMode {

                text = filteredCountriesList[indexPath.row]

            } else {

                text = countriesList[indexPath.row]
            }

            cell.congigureCell(text: text) // Error 2 happens here

            return cell

        } else {

            return UITableViewCell()
        }

    }

【问题讨论】:

  • configureCell 也许?
  • 如果有任何答案解决了您的问题,请接受该答案,以便以后的用户可以从中受益。

标签: ios swift compiler-errors


【解决方案1】:

1) !标记在末尾​​p>

countryList.dequeueReusableCell(withIdentifier: "cell")!

使用 force unwrap 使其成为非可选的,所以你不应该在 if let 中检查它,或者更好的方法是删除 !标记

2) configureCell 可能是不同类的方法,而不是UITableViewCell。你应该用这个类替换 UITableViewCell 来转换它

【讨论】:

  • 我怎样才能完成#2?我找到了解决 #1 的方法。
  • 您的 configureCell 可能是在某个类中定义的。将代码中的 UITableViewCell 更改为此类
【解决方案2】:

确保您已完成以下步骤。

  1. 将情节提要中的单元格标识符添加到您的自定义单元格中。即“细胞”
  2. 通过情节提要或代码将YourTableview 的委托和数据源分配给YOURViewController.swift
  3. YOURViewController.swift 使用表的数据源访问单元格 查看为。
  4. 添加子类 UITableViewCell 的自定义类并赋值给 故事板中的游览单元。

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

    if let cell = countryList.dequeueReusableCell(withIdentifier: "cell") as! YOURTableViewCellClass  {
    
        let text: String!
    
        if inSearchMode {
    
            text = filteredCountriesList[indexPath.row]
    
        } else {
    
            text = countriesList[indexPath.row]
        }
    
        cell.congigureCell(text: text) // Error 2 happens here
    
        return cell }
    

【讨论】:

    【解决方案3】:
    1. !标记用于强制展开可以为 nil 的可选值。但是“if let”和“guard let”已经检查了选项,所以你不需要!标记。

    随便用

    if let cell:UITableViewCell = countryList.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell
    
    1. 此行中的单元格是“UITableViewCell”,但 configureCell 不是 UITableViewCell 的成员。 如果您想使用自己的单元格(如 MyCell),则应将其转换为 MyCell。

    let myCell = cell as! MyCell

    【讨论】:

      【解决方案4】:

      1 . 而不是 dequeueReusableCellWithIdentifier: 使用 dequeueReusableCellWithIdentifier:forIndexPath: 后者从不提供 nil 值,因此您不必担心“nil 错误”。

      2.UItableViewCell 没有像您的情况那样配置单元格或配置单元格,而是您必须创建一个自定义 tableViewCell 并将函数添加为 configureCell() 然后在这一行中

      如果让 cell:UITableViewCell = countryList.dequeueReusableCell(withIdentifier: "cell")!作为 UITableViewCell

      as UITableViewCell 替换为 as yourCustomTableViewCellClass

      【讨论】:

        猜你喜欢
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-04
        • 2014-02-06
        • 2015-06-12
        相关资源
        最近更新 更多