将多个原型单元加载到UITableView中
我目前有一个UITableView包含2行的自定义单元格。 我最近添加了第二个原型单元格到我的故事板,并试图将其添加到我的UITableView没有成功。 我的cellForRowAtIndexPAth方法如下:
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“单元格”的原型单元格,但是当我去加载部分2时,我得到第一个原型单元格的另一个实例减去任何数据,因为它还没有被分配委托或数据源。 否则,单元格的ID分别设置为“单元格”和“单元格2”,但我似乎无法访问“单元格2”。
附加说明:我的故事板中有两个原型单元格,它们的设置都相同,它们的标识符都标记在相同的框中,从它们自己的类inheritance,并在我的UITableView中声明相同。 至于委托和数据源,我原来的原型单元格包含一个graphics(使用BEMSimpleLineGraph),这个单元格的每个实例都有它自己的委托和数据源,并在上面的代码中显示动作0和1。
下图(灰色)中的第一个单元格是保存graphics的原始单元格,而cell2正好位于其下方的白色单元格中。
我使用类似于你在cellForRowAtIndexPath
代码设置了一个testing应用程序,我得到了相同的结果。 即使input了我的if indexPath.section == 2
子句中的代码,返回的单元格看起来与我的前两个单元格相同(但标签中没有string)。 这是尽pipe我设置了不同的子视图,日志显示它是我想要的第2部分正确的类。为什么发生这种情况,我不知道,但要解决它,你需要什么要做的就是把你的单元里面的单元出队,就像这样。
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 }
}
这可以进一步重构取出重复的代码,但这应该工作…