gpt4 book ai didi

ios - 使用分段控件返回 cellForRowAtIndexPath 中的两个单元格

转载 作者:搜寻专家 更新时间:2023-11-01 07:33:41 24 4
gpt4 key购买 nike

我在两个 UITableViews 上方有一个分段控件,其中一个位于另一个之上。当 segmentedControl.selectedSegmentIndex == 1 时,其中一个 TableView 将隐藏。但是,问题是我无法在我的一个 cellForRowAtIndexPath 函数中配置第二个 TableView 的自定义单元格。我不断收到:Variable 'cell' used before being initialized

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

switch segmentedControl.selectedSegmentIndex {
case 0:
var cell = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as! CellOne

return cell

case 1:

var cellTwo = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as! CellTwo

return cellTwo

default:
var cell: UITableViewCell
return cell
}


}

最佳答案

您有默认分支,您返回的单元格变量未初始化。我建议进行如下更改:

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

if (segmentedControl.selectedSegmentIndex == 0) {
var cellOne = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as! CellOne
//Configure here
result = cellOne
} else {
var cellTwo = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as! CellTwo
//Configure here
result = cellTwo
}

return result
}

关于ios - 使用分段控件返回 cellForRowAtIndexPath 中的两个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31149688/

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