Ios 将多个原型单元加载到UITableView中
我当前有一个UITableView,其中包含两行自定义单元格。我最近在我的故事板中添加了第二个原型单元,并一直试图将其添加到我的UITableView中,但没有成功。我的cellForRowAtIndexPAth方法如下:Ios 将多个原型单元加载到UITableView中,ios,uitableview,swift,bemsimplelinegraph,Ios,Uitableview,Swift,Bemsimplelinegraph,我当前有一个UITableView,其中包含两行自定义单元格。我最近在我的故事板中添加了第二个原型单元,并一直试图将其添加到我的UITableView中,但没有成功。我的cellForRowAtIndexPAth方法如下: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: FlightsDetailC
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: FlightsDetailCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as FlightsDetailCell
cell.userInteractionEnabled = false
if indexPath.section == 0 {
cell.graphView.enableBezierCurve = true
cell.graphView.enableReferenceYAxisLines = true
cell.graphView.enableYAxisLabel = true
cell.graphView.colorYaxisLabel = UIColor.whiteColor()
cell.graphView.delegate = UIApplication.sharedApplication().delegate as BEMSimpleLineGraphDelegate
cell.graphView.dataSource = UIApplication.sharedApplication().delegate as BEMSimpleLineGraphDataSource
return cell
}
if indexPath.section == 1 {
cell.graphView.enableBezierCurve = true
cell.graphView.enableReferenceYAxisLines = true
cell.graphView.enableYAxisLabel = true
cell.graphView.colorYaxisLabel = UIColor.whiteColor()
cell.graphView.delegate = self
cell.graphView.dataSource = self
return cell
}
if indexPath.section == 2 {
let cell2: FlightsInformationCell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as FlightsInformationCell
cell2.userInteractionEnabled = false
return cell2
}
return cell
}
第0节和第1节正确加载了ID为“cell”的原型单元,但当我加载第2节时,我得到了第一个原型单元的另一个实例,没有任何数据,因为它没有被分配委托或数据源。否则,单元格的设置与ID分别为“Cell”和“Cell2”的设置相同,但我似乎无法访问“Cell2”
补充说明:我的故事板中有2个原型单元,它们的设置都是相同的,因为它们的标识符都标记在相同的框中,继承自它们自己的类,并且在我的UITableView中声明为相同的。对于委托和数据源,我的原始原型单元保存一个图(使用BEMSimpleLineGraph),该单元的每个实例都有自己的委托和数据源,如上面的操作0和1代码所示
下图中的第一个单元格(灰色)是保存图形的原始单元格,单元格2正下方为白色
我使用类似于您在
cellForRowAtIndexPath
中的代码设置了一个测试应用程序,得到了相同的结果。即使输入了ifindexPath.section==2
子句中的代码,返回的单元格看起来与前两个单元格相同(但标签中没有字符串);尽管我使用不同的子视图对其进行了设置,并且日志显示它是我想要的部分的正确类
2.我不知道为什么会发生这种情况,但要解决它,您需要做的是将if块中的单元排成这样
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
cell.userInteractionEnabled = false
if indexPath.section == 0 {
let cell: FlightsDetailCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as FlightsDetailCell
cell.graphView.enableBezierCurve = true
cell.graphView.enableReferenceYAxisLines = true
cell.graphView.enableYAxisLabel = true
cell.graphView.colorYaxisLabel = UIColor.whiteColor()
cell.graphView.delegate = UIApplication.sharedApplication().delegate as BEMSimpleLineGraphDelegate
cell.graphView.dataSource = UIApplication.sharedApplication().delegate as BEMSimpleLineGraphDataSource
return cell
}
else if indexPath.section == 1 {
let cell: FlightsDetailCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as FlightsDetailCell
cell.graphView.enableBezierCurve = true
cell.graphView.enableReferenceYAxisLines = true
cell.graphView.enableYAxisLabel = true
cell.graphView.colorYaxisLabel = UIColor.whiteColor()
cell.graphView.delegate = self
cell.graphView.dataSource = self
return cell
}
else {
let cell2: FlightsInformationCell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as FlightsInformationCell
cell2.userInteractionEnabled = false
return cell2
}
}
这可以进一步重构以去除重复的代码,但这应该是可行的…不知道如何在swift中处理这一点,但您可能应该重构代码,以便声明
单元格
,但在执行if/else语句中的代码之前,不要实际实例化/将其分配给非nil值。您的if/else应该更像if(indexPath.section<2){cell=cell1}else{cell=cell2}返回单元格
有趣,现在这在功能上是否与我当前正在做的有什么不同?如果返回值
通过,该方法将立即退出?您是否检查了cell2
是否已创建?(即,您是否输入了if分支?)我怀疑它不是,因此您只需返回在methodbest practices and readability开始时创建的单元格
对象。我发现代码重构也很平静:)另外,您是否也对这两个单元进行了子类化?我刚刚注意到你们有两个类,它们实际上都是在两个类中声明的,我希望一旦我有了这个工作哈哈。另外,确实创建了cell2
。感谢您的回复,结果很接近,但出于某种原因,我的第三个单元格的内容正确,但行高与cell
相同,而不是cell2
。目前,我已经使用heightforrowatinexpath
方法纠正了这个问题,尽管我觉得有点不对劲。否则,非常感谢你的帮助!虽然您的解决方案可行,但我认为有一种方法可以避免如此多的冗余代码。你能找到更好的解决办法吗?