无法返回cellForRowAtIndexPath中的单元格 [英] cant return cell in cellForRowAtIndexPath
问题描述
我正在尝试在tableView中返回不同的单元格。通常在这种情况下,我将返回不同的单元格,然后在底部返回nil,但在这种情况下,它给了我和错误。我也尝试过返回一个空单元格,但也给了我错误。
i'm trying to return different cells in a tableView. Normally in this case i would return different cells and then return nil at the bottom, but in this case it gives me and error. I've tried returning an empty cell as well, but also gives me and error.
我尝试了什么
return nil
和
var cell: UITableViewCell!
return cell
但两者都返回错误。我该如何解决这个问题?
but both returned errors. How can i fix this?
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
}
推荐答案
您不能从cellForRowAtIndexPath返回nil。你总是应该返回一个有效的单元格。
You mustn't return nil from cellForRowAtIndexPath. You always should return a valid cell.
var cell: UITableViewCell!
这行代码不会创建任何单元格,它只是一个带有nil内容的UITableViewCell变量,你必须为其赋值:
This line of code doesn't create any cell, it just a UITableViewCell variable with nil content, you have to assign a value to it:
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
return cell
这篇关于无法返回cellForRowAtIndexPath中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!