【发布时间】:2016-01-17 02:36:27
【问题描述】:
首先,我想指出我想要进行普通调用的原因,当可重用单元在基类中出列时,这是因为这行代码你很快就会在我的问题中再次看到:
cell.backgroundColor = cell.contentView.backgroundColor;
这是针对 iPad 的错误修复,不尊重我设置的 UITableView.backgroundColor = myCustomColor 和 UITableViewCell.backgroundColor = clearColor。 iPad 显示白色,iPhone 版本一切正常,你可以看到这个错误here,每次单元格出列时我都必须再次设置背景颜色,这是唯一适合我的解决方案。我试图在我的基类中这样做一次,并提出一个解决方案,我不必记住为每个子类调用一个 func(可能不可能)。
我有几个自定义的UITableViewControllers 类,我们称它们为ATableViewController 和BTableViewController,它们继承自一个名为UIBaseDashboardTableViewController 的基类,该基类继承自UITableViewController。
我正在生成动态原型表单元格并在ATableViewController 和BTableViewController 中使用以下函数:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ATableViewCellId", forIndexPath: indexPath)
//common setting to fix always white background in iPad bug
cell.backgroundColor = cell.contentView.backgroundColor;
return cell
}
对于ATableViewController 和BTableViewController,TableViewCell Id ACustomTableCellId 是唯一的或不同的。我的所有UITableViewControllers 都有一个通用设置,这些设置继承自我的基类UIBaseDashboardTableViewController。你可以看到上面代码的 backgroundColor 行是我的常用设置,在UIBaseDashboardTableViewController 的所有子类中都是相同的。在每个子类中,我首先尝试执行以下操作:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
super.tableView(tableView: tableView, cellForRowAtIndexPath: indexPath)
...
}
但这行不通,我需要ReusableCellIndentifer。
我目前的解决方案可能真的很好,如下所示,在我的子类中,我有以下内容:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let data = dataArray[indexPath.row]
let dequeuedCell = tableView.dequeueReusableCellWithIdentifier("BTableViewCellID", forIndexPath: indexPath)
let cell = dequeuedCell as! MyCustomTableViewCell
// Configure the cell...
cell.model = data
//call common settings for cells function in base class
super.setDequeueReusableCellCommon(cell)
return cell
}
然后在我的基类UIBaseDashboardTableViewController 中实现:
func setDequeueReusableCellCommon(cell: UITableViewCell) {
cell.backgroundColor = cell.contentView.backgroundColor
}
唯一的缺点是我必须记住在我所有的子班级中都打电话给super.v setDequeueReusableCellCommon。
关于如何解决这个问题有更好的建议吗?
【问题讨论】:
-
拥有相同的背景似乎过于复杂。你不想继承 UITableViewCell 的子类,因为共享的继承属性在那个对象而不是 UITableView 上?
-
它修复了 iPad 显示白色背景而不是我为 UITableView 背景颜色设置的颜色
标签: ios xcode swift uitableview