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
Related
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
I'm attempting to display a static tableview in my UITableViewController as I only want to display 1 row. I've set the tableview's content to Static Cells already but I still get more rows than I want to appear when I deploy it to my test device. I've also explicitly set the number of rows and sections in my UITableViewController subclass but still the results are the same.
I've attached some screenshots to further explain the result I'm getting.
Here's a screenshot of what my tableview controller looks like in the storyboard
Here's a screenshot of the attribute inspector of my table view
Here's a screenshot of my UITableViewController subclass
Finally, here's a screenshot of what the tableview looks like on the test device
As you can see even though I set the number of static rows to 1, I still get multiple rows. I'm not sure why this is happening but if anyone has any ideas, I'd appreciate the help.
Thanks
Change the table view's style to Grouped.
This should give the effect you desire:
An alternative to .grouped style, if you have to use .plain, is to attach a zero height tableFooterView to the table view since it won't draw separators below a footer.
You can either drag a view into the table view in Interface Builder, or add it in your viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
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.
I was looking at the answers in this question here -
Eliminate extra separators below UITableView
Remove unwanted ios7 padding
But I cannot understand why this works -
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
A footerView basically is at the end of the Table Contents if I'm correct. I also wanted a good idea of just why the separators are shown. Is it a default thing that iOS has when there is no cellForRowAtIndexPath? So it creates these default cells which are then overridden by the FooterView being present?
If you have a full-elements tableview and you enable the bounce property, you still will see other cells. This is because iOS SDK prepare other cell ready to be used when you will scroll and you will access to an another part of you dataSource.
Simply, the CGRectZero frame in the footer communicates that the table has a footer, so it will not probably continue. You can also create a footer not equal to the CGRectZero frame, cells will not be created anymore.
Apple Documentation
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];
}