I have this code for each index of my uitableview
if (indexPath.row == 6){
UIImageView *blog = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blog.png"]];
[cell setBackgroundView:blog];
UIImageView *selectedblog = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blogSel.png"]];
cell.selectedBackgroundView=selectedblog;
cell.backgroundColor = [UIColor clearColor];
[[cell textLabel] setTextColor:[UIColor whiteColor]];
return cell;}
and I have two sections, with 5 rows in each section. How can I put indexPath.row 1 thru 5 in section 1 and indexPath.row 6 thru 10 in section 2?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
Now your table view will expect 2 sections with 5 rows each, and try to draw them. Then, in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger actualIndex = indexPath.row;
for(int i = 1; i < indexPath.section; ++i)
{
actualIndex += [self tableView:tableView
numberOfRowsInSection:i];
}
// you can use the below switch statement to return
// different styled cells depending on the section
switch(indexPath.section)
{
case 1: // prepare and return cell as normal
default:
break;
case 2: // return alternative cell type
break;
}
}
The above logic with actualIndex
leads to:
If you have an underlying array (or other flat container class) of items backing your table cells, this will allow you to fill out multiple sections in the table view using those items.