How to customize UIView of sections in UITableView? - ios

I want to set the style my sections in a UITableView like viewForFooterInSection or viewForHeaderInSection.
I only can set the title using titleForHeaderInSection and height using heightForHeaderInSection but I want to set the baground color, font-size, font-color, etc...
It's posible do that?

you can do it with this delegate method whic provide by apple.
//for header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *myview = [[UIView alloc] init];
//give size for veiw, background color, add label anything here
return myview;
}
//for footer
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *myview = [[UIView alloc] init];
//give size for veiw, background color, add label anything here
return myview;
}

Related

How to change index based section common color in UITableView

Normally my screen display is like this:
When I use this code
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor whiteColor]];
return headerView;
}
my output is like this:
My section index text is not visible. I know that the section header view add my custom view. I want to change section header color and also display index text.
#Khawar answer i get this output
this is work 100% and you can change your index or header text color
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
view.tintColor = [UIColor blueColor];
// if you have index/header text in your tableview change your index text color
UITableViewHeaderFooterView *headerIndexText = (UITableViewHeaderFooterView *)view;
[headerIndexText.textLabel setTextColor:[UIColor blackColor]];
}
output:
Try to set the height for UITableView header.
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0;
}
If you only need to change the background color of section header while displaying section title, you don't need to create custom view for that. Custom view would overwrite your default header and your title would not be shown, unless you add custom UILabel in your custom view. Just use following UITableView delegate to change background color.
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
view.tintColor = [UIColor redColor];
}
See results before and after.
----------------Before------------------
----------------After------------------
Hope this fixes the issue.
You have to give title for section... Or you can use this method..
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headerView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
headerView.text = [self tableView:tableView titleForHeaderInSection:section];
return headerView;
}

How can I apply this header/footer margin between UITableViewCells?

This code creates a margin at the top and bottom of the UITableView. How can I modify it to do the same but between each individual UITableViewCell?
// set header height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10;
}
// set header colour
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor lightGrayColor];
return headerView;
}
// set footer height
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 10;
}
// set footer colour
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor lightGrayColor];
return headerView;
}
If successful, I'd also like to be able to add similar left and right margins if possible.
Use an extra Background UIView with top, left. bottom and right margin.[red background view in the picture]. Add all other subViews on this view.

IOS - Styling Section Headers in a static UITableViewController - IOS7

Is it possible to style a section header background / font style in a static UITableViewController? I cant see any storyboard option to do this and as the table is static the table methods have been disabled in the.M file - so I'm not sure how to apply styling programatically either?
The UITableViewDelegate protocol defines a method viewForHeaderInSection which can be implemented in your delegate to return custom header views (works for static UITableViews as well). e.g:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// you can get the title you statically defined in the storyboard
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
// create and return a custom view
UILabel *customLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 50.0f)];
customLabel.text = sectionTitle;
return customLabel;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
// return a custom height here if necessary
return 50.0f;
}

UITableView Background Image for Static Section/Cells

I know we can add background images/colors to section headers in dynamic table view/cells but can anyone help me do the same in a table view using Static Cells ?
What I want to do is use a background image for my 3rd section in a TableView which is using static cells (total of 4 sections it has)
I want to add a background image and change text color to say some RGB value for the 3rd section
You can use the delegate methods of UITableView to set the height and view for a section header. This should do what you want:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return (section == 2)? 100:30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
label.text = #"Description";
label.textColor = [UIColor whiteColor];
[imageView addSubview:label];
imageView.image = [UIImage imageNamed:#"House.tiff"];
return (section == 2)? imageView:nil;
}
UITableViewCells have the backgroundView property which is a UIView. You can change that to be a UIImageView, for example, or build a more complex background for you table view cells. It shouldn't matter, whether the cell is static or dynamic.
The text color for the cell can then simply be changed by setting the UITableViewCells cell.textLabel.textColor.
Same applies for the UITableViewHeaderFooterView, which are (as the name says) used for the header and footer of your table view sections. In code, you can access the header for a section using
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section
This saves you from completely recreating the header from scratch, just to make small adjustments. Alternatively, override
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
and build a custom UIView that will become your header.
You should assign a tag(say 15) to the static cell in
tableview:didSelectRowForIndexPath
and then
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(cell.tag==15)
cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:250/255.0 blue:243/255.0 alpha:1.0];
}

Delete the extra separator of UITableView in iOS 7

For my UITableView I have such code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
But when I launch me app, it shows an extra separator. It's not additional cell - it cannot be selected. How can it be fixed?
Eliminate extra separators below UITableView
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [UIView new];
// If you are not using ARC:
// return [[UIView new] autorelease];
}
With iOS7
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
It is by default in UITableView. You can use templates to create the kind of view you need, in an empty view controller (or) change the frame size of your UITableView in code by creating and allocating your own view.
You can also try to set footer view height to zero, by using next code in UITableViewDelegate class:
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0;
}

Resources