0

is it possible to change background image of each sections in uitableview?

I want to add background image for each sections in uitableview does anyone know how can I do that?

Thanks in advance!

like this picture --> put different background images for wednesday , Thursday and friday separately

enter image description here

Edit I want to add image 1 for wednesday image 2 for Thursday image 3 for friday and ..... how can I specify that ?

Edit

this the code for creating sections header I want to have background also

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if(section == 0) return @"Monday"; else if(section == 1){ return @"Tuesday"; }else if(section == 2){ return @"Wednesday"; } else if(section == 3){ return @"Thuesday"; } else if(section == 4){ return @"Friday"; } else if(section == 5){ return @"Saturday"; }else return @"Sunday"; } 

4 Answers 4

2

You could change the background in the cellForRowAtIndexPath delegate method based on the indexPath, like so:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"TaskCellRow"; UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; int maxRow = 3; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(indexPath.row, maxRow)]]]; } else { UIImageView *background = (UIImageView *)cell.backgroundView; background.image = [UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(indexPath.row, maxRow)]]; } return cell; } // To change header backgrounds - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { int maxRow = 3; UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(section, maxRow)]]]; return headerView; } 

You would then just create images, numbered for the desired amount header/rows, ie. background_image0.png, background_image1.png, background_image2.png, ... and so forth. The MIN will cap the amount off at the whatever you decide is the max backgrounds. Hope that helps.

EDIT: Changed cellForRowAtIndexPath based on Henri's comments. I overlooked that, thanks! This is for ARC compatibility.

Sign up to request clarification or add additional context in comments.

8 Comments

Your example completely ignores the case where a cell that was created for section N is reused as part of section N+1. Meaning this example does not actually work, at least most of the time anyway. Also, backgroundView is a property of type (nonatomic, retain), so you are actually leaking memory there. Same goes to the header background example, the method expects an autoreleased object as the return value, so you are leaking memory there as well.
Note, my comment follows from the fact that you did not specify the example as ARC compatible code, if that's the case, then the leaks are not present.
@HenriNormak Edit I want to add image 1 for wednesday image 2 for Thursday image 3 for friday and ..... how can I specify that ?
You still have a minor bug when cell == nil, you can't have initWithImage: and give it an NSString object, it needs to be an UIImage.
It should be [[UIImageView alloc] initWithImage: [UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(indexPath.row, maxRow)]], not the [[UIImageView alloc] initWithImage:[NSString stringWithFormat:@"background_image%i.png", MIN(indexPath.row, maxRow)]] it is right now
|
1

You can use:

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

to specify any kind of UIView for a section header. This other delegate method:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 

lets you specify the height.

Just alloc/init the UIView in the first one using the table's width and the height from the second method and then add any number of views to it, such as a UIImageView for a background then a label for the title.

Comments

0

Iterating (and mostly correcting) example given by ezekielDFM. Note, this code is not ARC compatible (which previous example may have been).

// To change cell backgrounds -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"TaskCellRow"; UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; int maxRow = 3; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; UIImageView *imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(indexPath.row, maxRow)]]] autorelease]; cell.backgroundView = imageView; } else { // Reusing, we need to swap out the image of the background cell.backgroundView.image = [UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(indexPath.row, maxRow)]]; } return cell; } // To change header backgrounds - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { int maxRow = 3; UIImageView *headerView = [[[UIImageView alloc] initWithImage:[NSString stringWithFormat:@"background_image%i.png", MIN(section, maxRow)]] autorelease]; return headerView; } 

2 Comments

I want background for sections not for cells
You don't understand, there is no "section" per se, there are cells that look like sections, changing a background on all the cells in a section == changing the background of the section. Other option would be to mess about with the scrollView and add the suitable backgrounds, but that is way more difficult than the suggested route.
0

As i guess it is not possible to change the background image for Table view Sections. If you want to do such a requirement, please try with cell for row as Sections. i.e. for each row treat as section of table & in that add 1 more table view with different tag. It will full your req. or else for each row take

Comments