I want set the background image for cell. I h开发者_JS百科ave already set the background images for the cells in the table view. But I want to set the two images for the background images in the table view cell. I want to set the alternate cells for the background,
Like,
Image1- Cell 1, Cell 3, Cell 5, Etc.,
Imgae-2- Cell 2, Cell 4,Cell 6, Etc.,
Please guide me and give me some sample links.
Thanks!
You can setup two different cells for even/uneven rows like below
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifierEvenRows = @"MyCellEven";
static NSString *CellIdentifierUnevenRows = @"MyCellUneven";
UITableViewCell *cell;
if (indexPath.row % 2) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierUnevenRows];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifierUnevenRows] autorelease];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"uneven.png"] autorelease];
}
} else {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierEvenRows];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifierEvenRows] autorelease];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"even.png"] autorelease];
}
}
return cell;
}
(not tested though)
精彩评论