调用super.tableView(tableView:UITableView,cellForRowAtIndexPath ... with reuseIdentifier

时间:2016-01-17 02:36:27

标签: ios xcode swift uitableview

首先,我想指出我想要进行公共呼叫的原因,当可重用的单元格在基类中出列时,由于这行代码,您将很快再次看到我的问题:

cell.backgroundColor = cell.contentView.backgroundColor; 

这是一个错误修复iPad不尊重我设置的UITableView.backgroundColor = myCustomColor和UITableViewCell.backgroundColor = clearColor。 iPad显示白色,iPhone版本一切都很好,你可以看到这个bug here,每次单元格出列时我都要再次设置背景颜色,这是唯一适用于我的解决方案。我试图在我的基类中执行一次,并提出一个解决方案,我不必记住为每个子类调用一个函数(可能不可能)。

我有几个自定义UITableViewControllers类,让我们称之为ATableViewControllerBTableViewController,它们从名为UIBaseDashboardTableViewController的基类继承而来,继承自{{ 1}}。

我正在生成动态原型表格单元格,并在UITableViewControllerATableViewController中使用以下功能:

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 } ACustomTableCellId,TableViewCell标识ATableViewController是唯一的或不同的。对于从我的基类BTableViewController继承的所有UITableViewControllers,我有一个通用设置。您可以看到上面的backgroundColor代码行是我的常用设置,它在UIBaseDashboardTableViewController的所有子类中都是相同的。在每个儿童班,我首先尝试做以下事情:

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 }

关于如何解决这个问题的更好的建议?

1 个答案:

答案 0 :(得分:4)

您正在更改单元格的背景颜色,但是为tableViews进行了继承。对tableViewCell使用继承,而不是整个tableView。在awakeFromNib方法中的tableViewCell设置{{1}}的根类中。

相关问题