【问题标题】:cant return cell in cellForRowAtIndexPath无法在 cellForRowAtIndexPath 中返回单元格
【发布时间】:2014-10-02 14:10:25
【问题描述】:

我正在尝试在 tableView 中返回不同的单元格。通常在这种情况下,我会返回不同的单元格,然后在底部返回 nil,但在这种情况下,它会给我和错误。我也尝试过返回一个空单元格,但也给了我和错误。

我试过的

return nil

 var cell: UITableViewCell!
  return cell

但两者都返回错误。我该如何解决这个问题?

cellForRowAtIndex

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    
    
    
    if indexPath.row == 0 {


        let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as UITableViewCell
        var imageFile = cell.viewWithTag(100) as PFImageView
        imageFile.image = itemFile


        cell.selectionStyle = UITableViewCellSelectionStyle.None
        return cell
        
    } else if indexPath.row == 1 {
        let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("segmentCell", forIndexPath: indexPath) as UITableViewCell
        
        var titleLabel = cell.viewWithTag(101) as UILabel?
        titleLabel?.text = itemTitle
        
        let segmentControl = cell.viewWithTag(102) as UISegmentedControl
        segmentControl.selectedSegmentIndex = segment
        segmentControl.setTitle("Beskrivelse", forSegmentAtIndex: 0)
        segmentControl.setTitle("Sælger", forSegmentAtIndex: 1)
        segmentControl.setTitle("Lokation", forSegmentAtIndex: 2)
        segmentControl.tintColor = UIColor(rgba: "#619e00")
        var font = UIFont(name: "Lato-Regular", size: 11)
        var attributes:NSDictionary = NSDictionary(object: font , forKey: NSFontAttributeName)
        segmentControl.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
        segmentControl.addTarget(self, action: "segmentAction:", forControlEvents: .ValueChanged)
        

        cell.selectionStyle = UITableViewCellSelectionStyle.None
        
        return cell
      
    } else if indexPath.row == 2 {
        switch segment {
        case 0:
            let cell = tableView.dequeueReusableCellWithIdentifier("CellZero", forIndexPath: indexPath) as DescViewCell
            return cell
        case 1:
            let cell = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as SellerViewCell
            return cell
        case 2:
            let cell = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as LocationViewCell
            return cell
        default:
            break
        
        }
    }
        
    
    var cell: UITableViewCell!
    return cell
}

【问题讨论】:

    标签: ios uitableview swift


    【解决方案1】:

    previous answer for a similar question 中所示,-tableView:cellForRowAtIndexPath: 必须返回UITableViewCell。所以你不能返回nil。但是,当您在其中使用 if elseswitch 语句时,我还建议避免在 -tableView:cellForRowAtIndexPath: 末尾返回以下代码:

    //bad code design
    var cell: UITableViewCell!
    return cell
    

    或:

    //bad code design
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    return cell
    

    您可以编写更好的代码,而不是创建一个从不调用的 UITableViewCell 实例,只是为了消除 Xcode 警告!

    那么解决办法是什么?

    关键是要确保if else 语句的最后一个可能值设置在else(而不是else if)中。同样,关键是要确保switch 语句的最后一个可能值设置在default:(而不是case XXX:)中。

    因此,您的代码应如下所示:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("CellZero", forIndexPath: indexPath) as UITableViewCell
            /* ... */
            return cell
        } else if indexPath.row == 1 {
            let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as CellOne
            /* ... */
            return cell
        } else { //set your last indexPath.row case in "else", not in "else if indexPath.row == 2"!!!
            switch segment {
            case 0:
                let cell = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as CellTwo
                /* ... */
                return cell
            case 1:
                let cell = tableView.dequeueReusableCellWithIdentifier("CellThree", forIndexPath: indexPath) as CellThree
                /* ... */
                return cell
            default: //set your last segment case in "default:", not in "case 2:"!!!
                let cell = tableView.dequeueReusableCellWithIdentifier("CellFour", forIndexPath: indexPath) as CellFour
                /* ... */
                return cell
            }
        }
        //No need for a fictive "return cell" with this code!!!
    }
    

    如果segment 不是可选的,感谢元组,您甚至可以将之前的代码简化为:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        switch (indexPath.row, segment) {
        case (0, _):
            let cell = tableView.dequeueReusableCellWithIdentifier("CellZero", forIndexPath: indexPath) as UITableViewCell
            /* ... */
            return cell
        case (1, _):
            let cell = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as CellOne
            /* ... */
            return cell
        case (2, 0):
            let cell = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as CellTwo
            /* ... */
            return cell
        case (2, 1):
            let cell = tableView.dequeueReusableCellWithIdentifier("CellThree", forIndexPath: indexPath) as CellThree
            /* ... */
            return cell
        default: //case (2, 2)
            let cell = tableView.dequeueReusableCellWithIdentifier("CellFour", forIndexPath: indexPath) as CellFour
            /* ... */
            return cell
        }
    }
    

    【讨论】:

    • default: //case (2, 2) 位感觉不优雅。也许使用fallthrough?但我什至不喜欢那样。
    【解决方案2】:

    你不能从 cellForRowAtIndexPath 返回 nil。你总是应该返回一个有效的单元格。

     var cell: UITableViewCell!
    

    这行代码并没有创建任何单元格,它只是一个内容为零的 UITableViewCell 变量,你必须为其赋值:

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

    【讨论】:

      【解决方案3】:

      你只是声明了最后一个var cell: UITableViewCell! - 你没有初始化它。你需要做类似的事情

      var cell = UITableViewCell(style: someStyle, reuseIdentifier: someID)
      return cell
      

      【讨论】:

        【解决方案4】:

        您需要在 if...then 逻辑之前声明单元格,然后在之后返回它。如果使用var,则不必初始化单元格:

        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
            var cell: UITableViewCell!
        
            if indexPath.row == 0 {
                cell = tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as UITableViewCell
                // ...
            } else if indexPath.row == 1 {
                cell = tableView.dequeueReusableCellWithIdentifier("segmentCell", forIndexPath: indexPath) as UITableViewCell
                // ...
            } else if indexPath.row == 2 {
                // ...
            } 
        
            return cell
        }
        

        (只要确保你捕捉到所有情况——如果你的返回单元没有初始化你会得到一个运行时错误。)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-09
          • 2011-10-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-24
          • 2012-01-31
          相关资源
          最近更新 更多