【问题标题】:swift multiple cell subclasses in UITableViewCOntroller在 UITableViewCOntroller 中快速创建多个单元格子类
【发布时间】:2014-11-26 06:24:29
【问题描述】:

我正在尝试将多个子类添加到 UITableView。问题是它一直给我以下错误:

Type UITableVieCell does not conform to protocol NilLiteralConvertible

CellForRowAtIndexPath

override  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.section == 0 {

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = self.section1[indexPath.row]
        cell.accessoryType = .DisclosureIndicator

        return cell

    } else if indexPath.section == 1 {

        let cell = tableView.dequeueReusableCellWithIdentifier("SwitchViewCell", forIndexPath: indexPath) as SwitchViewCell

        cell.cellLabel?.text = self.section2[indexPath.row]

        return cell
    }

    return nil
}

【问题讨论】:

    标签: ios objective-c iphone uitableview swift


    【解决方案1】:

    你不能返回零。而是默认返回空单元格。

        var cell :UITableViewCell!
    
        switch (indexPath.row) {
        case 0:
            cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
            cell.textLabel?.text = self.section1[indexPath.row]
            cell.accessoryType = .DisclosureIndicator
            break;
    
        case 1:
            cell = tableView.dequeueReusableCellWithIdentifier("SwitchViewCell", forIndexPath: indexPath) as SwitchViewCell
            (cell as SwitchViewCell).cellLabel?.text = self.section2[indexPath.row]
            break;
    
        default:
            break;
        }
    
        return cell
    

    确保您已注册笔尖

     self.tableView.registerNib(UINib(nibName: "SwitchViewCell", bundle: nil), forCellReuseIdentifier: "SwitchViewCell")
    

    【讨论】:

      【解决方案2】:

      tableView:cellForRowAtIndexPath: 必须返回 UITableViewCell 并且不能返回 nil。所以你必须删除return nil。但这还不够。您的if else 声明也必须完整。这意味着必须提供每个可能的 section 值,或者至少发送到失败。

      您的if else 语句应如下所示:

      if indexPath.section == 0 {
          /* ... */
      } else if indexPath.section == 1 { 
          /* ... */
      } else {
          /* ... */
      }
      

      或者像这样:

      if indexPath.section == 0 {
          /* ... */
      } else {
          /* ... */
      }
      

      但是,下面的if else 语句并不完整:

      if indexPath.section == 0 {
          /* ... */
      } else if indexPath.section == 1 { 
          /* ... */
      }
      

      在这种情况下,tableView:cellForRowAtIndexPath: 将不知道如果这些条件中的任何一个得到验证,将返回什么。因此,如果您尝试它,Xcode(即智能)也会显示错误消息:

      预期返回“UITableViewCell”的函数中缺少返回

      因此,以下代码应该可以工作:

      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
          if indexPath.section == 0 {
              let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
      
              cell.textLabel?.text = self.section1[indexPath.row]
              cell.accessoryType = .DisclosureIndicator
      
              return cell
          } else {
              let cell = tableView.dequeueReusableCellWithIdentifier("SwitchViewCell", forIndexPath: indexPath) as SwitchViewCell
      
              cell.cellLabel?.text = self.section2[indexPath.row]
      
              return cell
          }
      }
      

      PS:

      如果您还在tableView:cellForRowAtIndexPath: 方法中使用switch 语句来设置您的单元格,this answer to a similar question 可能会对您有所帮助。

      【讨论】:

        【解决方案3】:

        在我的情况下,我做了以下事情:

        let cellOne = mTableView.dequeueReusableCell(withIdentifier: "CellOne") as! customTableViewCellOne
        let cellTwo = mTableView.dequeueReusableCell(withIdentifier: "CellTwo") as! customTableViewCellTwo
        
        if (..something...) { 
            cellOne.mLabel = "hey"
            return cellOne
        }
        
        else if (..another condition..) {
            cellTwo.mButton.layer.cornerRadius = 5
            return cellTwo
        }
        return UITableViewCell()
        

        希望对你有帮助

        【讨论】:

          【解决方案4】:

          你不能为 cellForRowAtIndexPath 返回 nil

          你可以在最后使用:

          let cell:UITableViewCell!
          return cell
          

          而不是

           return nil
          

          这应该(如果你只有 2 个部分)永远不会被执行。

          编辑:

          您也可以使用 "} else if indexPath.section == 1 {" only } else { - 来代替单元格。我只是显示出了什么问题。或者使用 Switch/Case 并默认返回一个空单元格。

          【讨论】:

          • 他也可以用 "} else if indexPath.section == 1 {" only } else { - 返回一个单元格。我只是显示出了什么问题。或者使用 Switch/Case 并默认返回一个空单元格。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-01
          • 1970-01-01
          • 2019-07-18
          • 1970-01-01
          相关资源
          最近更新 更多