我目前有一个 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 为“Cell”的原型单元,但是当我去加载第 2 部分时,我得到第一个原型单元的另一个实例,缺少任何数据,因为它尚未分配委托或数据源。否则,单元格的设置分别与“Cell”和“Cell2”的 ID 相同,但我似乎无法访问“Cell2”。
附加说明:我的故事板中有 2 个原型单元,它们的设置相同,因为它们的标识符都标记在相同的框中,从它们自己的类继承,并且在我的 UITableView 中声明为相同的。至于委托和数据源,我的原始原型单元包含一个图形(使用 BEMSimpleLineGraph),该单元的每个实例都有它自己的委托和数据源,如上面的操作 0 和 1 的代码所示。
下图所示的第一个单元格(灰色)是保存图表的原始单元格,而单元格 2 就位于它的下方(白色)。
我使用类似于您的代码设置了一个测试应用程序cellForRowAtIndexPath
,我得到了相同的结果。即使我的 if 中的代码indexPath.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
}
}
这可以进一步重构以删除重复的代码,但这应该可行......
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)