Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help needed] Problems in changing underlying TableViewCell label #12

Closed
karstengresch opened this issue Jan 21, 2018 · 3 comments
Closed

Comments

@karstengresch
Copy link

Thanks for this wonderful project first - seems to make life much easier!

I'd like to color the labels of QuickTableViewController's rows, but can't get it to work.

Though I'm able applying a general style in AppDelegate with...

  let tableViewCellLabelAppearance = UILabel.appearance(whenContainedInInstancesOf: [UITableViewCell.self])
  tableViewCellLabelAppearance.textColor = Theme.textColor

...all other labels in all other TableViewCells get colored accordingly, except of those from QuickTableViewController.

My naive approach to get this working was...

  let optionRowLabelAppearance = UILabel.appearance(whenContainedInInstancesOf: [OptionRow.self])
  optionRowLabelAppearance.textColor = Theme.textColor

...but this was refused with the compiler error message:

Cannot convert value of type 'OptionRow<UITableViewCell'>.Type to expected element type 'UIAppearanceContainerType'

Wondering now what the preferred way is theming QuickTableViewController views?

Thanks and best wishes,

Karsten

@bcylin
Copy link
Owner

bcylin commented Jan 22, 2018

Hi Karsten,

Thanks for the feedback. The label is contained in the associated UITableViewCell, instead of OptionRow itself. To fix the compiler error, it should be:

// When it's OptionRow<CustomOptionCell>
let optionRowLabelAppearance = UILabel.appearance(whenContainedInInstancesOf: [CustomOptionCell.self])
optionRowLabelAppearance.textColor = Theme.textColor

UIAppearance issue

I find that UILabel.appearance(whenContainedInInstancesOf: [UITableViewCell.self]) works alright when the cell is dequeued with an identifier set in the storyboard.

But somehow it doesn't work when the cell is either registered with a class:

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")

or initialized in code:

let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "reuseIdentifier")

Since QuickTableViewController initializes table view cells in code without using a storyboard, the labels in cells don't pick up the appearance settings. One way to work around this issue might be registering a nib object.

Workaround with a nib object

Add a CustomOptionCell.xib:

  1. drag in a Table View Cell object to replace the default view
  2. set its Style to Basic or Subtitle
  3. set the Custom Class to CustomOptionCell
  4. set the reuse identifier to CustomOptionCell, same as the type name

Register the nib to the table view with:

class CustomOptionCell: UITableViewCell {}
let typeName = "CustomOptionCell"
tableView.register(UINib(nibName: typeName, bundle: .main), forCellReuseIdentifier: typeName)

Instead of using the default OptionRow in the section, specify the associated cell type OptionRow<CustomOptionCell>.

Then the table view will dequeue the cell registered in the nib and the UIAppearance customization should work in those cells. Hope this helps.

@karstengresch
Copy link
Author

Hi Ben,

thanks for your quick answer. So much appreciated!

Then the table view will dequeue the cell registered in the nib and the UIAppearance customization should work in those cells.
Hope this helps.
This indeed helped and I could successfully change the label color 👍

Unfortunately, the problem remains with SwitchCell and TapActionCell - got stuck creating a xib file directly using TapActionCell eg.

There should be an easier way to get the labels styled (maybe using

public init(style: UITableViewCellStyle, reuseIdentifier: String?)

? But I'm not really sure, how.

Any idea at your side?

Best wishes,

Karsten

@bcylin
Copy link
Owner

bcylin commented Jan 23, 2018

Hi Karsten,

I tried it on the Example app.
https://github.com/bcylin/QuickTableViewController/compare/feature/ui-appearance

Although TapActionCell sets its text color during initialization, it seems no problem with the UIAppearance textColor on iOS 11.2 simulator.

@bcylin bcylin closed this as completed Jan 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants