Customize TableviewCell depending on section
How do I customize the background image of a UITableViewCell
depending on the section, they are in? E.g. in section one, the cells are supposed to have a green .png image as a ba开发者_运维百科ckground image, whereas in section two, the cells are supposed to have a red .png image as background.
See Populating the Table View With Data for how to access and use section information.
When you have more then one section you implement some optional table view source methods to deal with sections.
Then when you create cell, check in which section it is and set it up accordingly, in:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath;
In this method you can access indexPath.section
and format your cell.
See Table View Programming Guide for more.
UIKit adds property section
to NSIndexPath
so you can get section
from index path passed in to the above method (see the first example I linked).
example:
if(section == 0) {
background.image = [UIImage imageNamed:@"green.png"];
}
else if(section == 1) {
background.image = [UIImage imageNamed:@"blue.png"];
}
精彩评论