Reputation: 375
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
Upvotes: 0
Views: 1502
Reputation: 1183
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];
}
Upvotes: 0
Reputation: 397
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.
Upvotes: 0
Reputation: 104082
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;
}
Upvotes: 1