无法返回cellForRowAtIndexPath中的单元格

时间:2014-10-02 14:10:26

标签: ios uitableview swift

我正在尝试在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
}

4 个答案:

答案 0 :(得分:5)

previous answer for a similar question所示,-tableView:cellForRowAtIndexPath:必须返回UITableViewCell。所以你不能返回nil。但是,我还建议您在{em> if else 或切换语句中时,避免在-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
    }
}

答案 1 :(得分:2)

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

 var cell: UITableViewCell!

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

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

答案 2 :(得分:1)

您只是声明了最后一个var cell: UITableViewCell! - 您没有初始化它。你需要做一些像

这样的事情
var cell = UITableViewCell(style: someStyle, reuseIdentifier: someID)
return cell

答案 3 :(得分:1)

您需要在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
}

(只要确保你抓住所有情况 - 如果没有初始化你的返回单元将会出现运行时错误。)