1

First, I wanted to point out the reason I want to make a common call, when a reusable cell is dequeued in a base class, it's because of this line of code you will see again soon further in my question:

cell.backgroundColor = cell.contentView.backgroundColor; 

This is a bug fix for iPad not respecting the UITableView.backgroundColor = myCustomColor and UITableViewCell.backgroundColor = clearColor I have set. iPad displays white instead, everything is fine in iPhone versions, you can see this bug here, I have to set the background color again each time the cell is dequeued that is the only solution that works for me. I am trying to do this once in my base class, and come up with a solution where I do not have to remember to call a func for every child class (might not be possible).

I have a couple custom UITableViewControllers classes, let's call them ATableViewController and BTableViewController they inherit from a base class called UIBaseDashboardTableViewController which inherits from UITableViewController.

I am generating dynamic Prototype Table cells and making use of the function below in ATableViewController and 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
    }

The TableViewCell Id ACustomTableCellId is unique or different for ATableViewController and BTableViewController. I have a common setting for all my UITableViewControllers that inherit from my base class, UIBaseDashboardTableViewController. You can see the backgroundColor line of code above is my common setting that will be the same in all child classes of UIBaseDashboardTableViewController. In each child class I first tried to do the following:

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    super.tableView(tableView: tableView, cellForRowAtIndexPath: indexPath)
    ...
}

But that is not going to work, I need the ReusableCellIndentifer.

My current solution, which really is just fine probably, is the following, in my child classes I have the following:

 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
    }

And then in my base class UIBaseDashboardTableViewController I implemented:

func setDequeueReusableCellCommon(cell: UITableViewCell) {
        cell.backgroundColor = cell.contentView.backgroundColor
    }

The only downside to this is that I have to remember to call super.v setDequeueReusableCellCommon in all my child classes.

Any better suggestions on how solve this?

Community
  • 1
  • 1
Brian Ogden
  • 16,278
  • 7
  • 77
  • 156
  • This seems overly complex to have the same background. Wouldn't you want to base subclass UITableViewCell as the shared inherited property is on that object not UITableView? – beyowulf Jan 17 '16 at 02:47
  • Its a bug fix for iPad displaying white background instead of the color I have set for the UITableView background color – Brian Ogden Jan 17 '16 at 02:49

1 Answers1

4

You are changing background color for cell, but made inheritance for tableViews. Use inheritance for tableViewCell, not the whole tableView. And in root class for tableViewCell setup self.backgroundColor = self.contentView.backgroundColor in awakeFromNib method.

shpasta
  • 1,833
  • 12
  • 21