gpt4 book ai didi

ios - 无法返回 cellForRowAtIndexPath 中的单元格

转载 作者:搜寻专家 更新时间:2023-11-01 05:49:14 26 4
gpt4 key购买 nike

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

最佳答案

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

您可以编写比创建一个永远不会被调用只是为了让 Xcode 警告静音的 UITableViewCell 实例更好的代码!

那么解决方案是什么?

关键是要确保 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 不是可选的,多亏了 tuples,您甚至可以将之前的代码简化为:

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
}
}

关于ios - 无法返回 cellForRowAtIndexPath 中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26162711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com