Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any way to set the UITableView section header color in iOS 7 directly rather creating a new UIView

UITableView's section headers are less identifiable in iOS 7. Are there any way to set the UITableView section header color in iOS 7 directly rather creating a new UIView.

enter image description here

Note: I found some solutions by creating a new UIView in,

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

but I really wanted to keep the Apple's properties except color. Are there any way to do it without this method.

like image 614
Goppinath Avatar asked Jan 21 '14 11:01

Goppinath


People also ask

How to change the background and text color of UITableView section?

Set the background and text color of section area: (Thanks to William Jockusch and Dj S) To change the background color, text label color and font for the Header View of a UITableView Section, simply override willDisplayHeaderView for your table view like so:

What is the difference between the Settings app and UITableView?

For example, the Settings app uses tables and a navigation controller to organize the system settings. UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content.

How to add sections and index list in the UITableView?

Basically, to add sections and an index list in the UITableView, you need to deal with these methods as defined in UITableViewDataSource protocol: numberOfSectionsInTableView: method – returns the total number of sections in the table view. Usually we set the number of section to 1.

How to add header and footer in UITableView?

There are two ways that we can add the header and footer, one way is to add it in using Interface Builder and the other way is to do it with code. In this tutorial we will be using both Interface Builder as well as code. The first thing that we need to do is to set up the UITableView and get it to show some data.


2 Answers

Implement tableView:willDisplayHeaderView:forSection: and update the view that you are passed.

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    UITableViewHeaderFooterView *v = (UITableViewHeaderFooterView *)view;
    v.backgroundView.backgroundColor = [UIColor darkGrayColor];
}

(assuming that you supplied a UITableViewHeaderFooterView instance in your implementation for tableView:viewForHeaderInSection:)

like image 108
Wain Avatar answered Sep 19 '22 14:09

Wain


In the case of Swift 3 the way is to achieve it via UITableViewDelegate method:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView  {
        headerView.backgroundColor = UIColor.gray
    }
}
like image 39
Darius Miliauskas Avatar answered Sep 19 '22 14:09

Darius Miliauskas