How to control UITableViewCell's label appearance via UIAppearance? - ios

I'm using a UITableView from an external library. This table view creates UITableViewCells using style: .default. I want to change the appearance of the cell, most importantly the cell's label text color, without resorting to changes to that library.
I've tried the following, unsuccessfully:
UITableViewCell.appearance().textLabel?.textColor = .systemBlue
UITableViewCell.appearance().tintColor = .systemPink
UILabel.appearance(whenContainedInInstancesOf: [UITableViewCell.self]).textColor = .systemTeal
Text color remains black. (I'm aware that cells use the internal UITableViewCellLabel class, and haven't found a way to access its appearance proxy)
Interestingly, UILabel.appearance().textColor = .systemGreen does affect the appearance of table section headers in a grouped table, but only for those sections that initially appear on-screen. Scrolling the table so that new sections appear shows their headers with black text labels, and through reuse, scrolling back to the original sections redisplays these headers with black text as well.
What are my options?

Table views, cells, headers, footers, etc can be quirky. UIKit does a lot of behind-the-scenes work, particularly with the default properties.
You have a couple options:
You can set the label's text color in either cellForRowAt or willDisplay cell, or...
Create a custom table view cell. You can then use the appearance proxy:
UILabel.appearance(whenContainedInInstancesOf: [MyBasicCell.self]).textColor = UIColor.red

Related

How to set UITableView background for blank space?

I attempted to use UITableView (with static content) to create a setting screen similar to the iPhone's system setting screen. It mostly works excepts the blank space under the last row are filled with white row cells instead of background color, as shown in this screenshot:
I have tried various ways to fix this: setting the table view's background, add an empty row as the last row with the background color. What should be the right fix?
Objective-C
[[UITableView alloc] initWithStyle: UITableViewStyleGrouped]
Swift 2
UITableView(style: .Grouped)
Swift 3
UITableView(style: .grouped)
You should also be able to set the table view style in the interface builder.
You need:
tableView.tableFooterView = UIView()
How to remove empty cells in UITableView?
Eliminate extra separators below UITableView
Go to View Section of the TableView's Attributes Inspector and set the background color there. It will work

spacing between cells io9.2 objective C

I want to create spacing between table view cells like this:
I have 1 section and a dynamic number of rows per section for my tableview. So far I have set my content view to clear like this in my tableView:cellForRowAtIndexPath: method:
cell.contentView.backgroundColor = [UIColor clearColor];
I have also created a view within my content view, but I'm not sure how to set those values or manipulate the subview so that it appears as though there is spacing. If there's an easier way to create spacing I'm open to those suggestions as well.
Set the group background to clear color and the controller's background to the color you want.
1) Set the tableviews background colour to clear.
2) Set the cells background colour to clear as well.
3) Add a view(suppose "myView") with the background colour you need on top of the cell such that the height of the view is less than the rows height (e.g. myView.height = 90 and row height is 100).
4) Add the rest of your views as subviews of myView.
You will achieve your desired result.

ios tableView clearColor seperator

I have set my tableView's background color, and I want there is space(10px) around each cell show that color.
e.g.(ignore the red line)
What I am doing now is make the cell embed a smaller view, and set the cell's background to be clear. But it is annoying to create such embed view again and again.
So, I am seeking for a better solution, I believe it would be good if i can increase separator's height. But the old answers did not solve my problem.
(How to increase the UITableView separator height?)
EDIT:
However, if I add my own separator in cell. I may not able to set the cell having round corner
Set the tableView's separatorStyle to .None.
Create a custom UITableViewCell subclass.
In the storyboard (or xib if you created one for the custom cell), add a subview that's inset the amount that you want padded on all four sides.
If you want rounded views, do that on the custom cell's subview (e.g. subview.layer.cornerRadius = 10.0, subview.layer.masksToBounds = true).
Set the custom cell's ContentView to have a clear background.
Set the table view's background to that lovely shade of blue.
Set the cell subview's background color to white.
You don't have to use the cell repeatedly in InterfaceBuilder. Just give the cell an ID, and in your implementation of the table view's data source, dequeue a cell instance using that ID and set the values of the labels inside it.
Have you thought of just making the cell height taller and putting your own separator on the bottom of your custom cells? Nice table layout by the way.

Change UITableViewCell background color in Interface builder

I'm trying to avoid adding any code to my iOS project that has to do with visual set up since that's what Interface Builder is supposed to streamline for you.
So I'm trying to set up the prototype cell in IB and then just call:
TableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"TableCell"];
The problem is if I change the background color for the cell in IB it remains white when I run my application. I can preset the color of the label fine but not the background color attribute in the Table Cell.
As an additional note, I can see that the UIView that is the backgroundView inside the cell is null after breakpointing and inspecting the variable after it's been initialized.
UPDATE
I'm getting the impression that the option for background color in IB is essentially useless? I'd rather not believe that.
You cannot do that unfortunately.
Change the background colour of the cell in tableView:willDisplayCell:forRowAtIndexPath:
From the documentation of UITableViewCell:
Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source. Changes to the background colors of cells in a group-style table view has an effect in iOS 3.0 that is different than previous versions of the operating system. It now affects the area inside the rounded rectangle instead of the area outside of it.
In interface builder, add a view as a subview of the cell, that will be your background view. You can then add all labels or anything else as subviews to that view. Change that views background color as you want.
I think this is probably a bug in IB. You can add views to that bar at the bottom of the controller, and then connect those views to the backgroundView and selectedBackgroundView properties of the cell. However, the backgroundView's color doesn't work, while the selectedBackgroundView's color does work correctly. If you add a background view in code to a custom table view cell and give it a backgroundColor, that does work.

Having an empty iOS tableview look (but when filled this changes)?

Hi I am attempting to create a rectangular region (tableView), that when it has no cells will be just a background color. Then when it has cells, the cell background can be different and separators become visible. This idea can be seen in http://itunes.apple.com/us/app/spendings/id473857206?mt=8
in the second screen shot. I can come close by just setting the background color of the table and the cells to different colors, but when there are no cells the table collapses. The screenshot in that link doesn't show this, but when there are no items, it is just an empty rectangle. How can one maintain the dimensions of the table, even when there are no cells?
You can try setting the separator color to clearColor so that the cells aren't visible when they're empty, like:
if([tableView numberOfRowsInSection:0]==0)
{
tableView.separatorColor = [UIColor clearColor];
}

Resources