简体   繁体   中英

How do I set background image in the table view cell in iPhone

I want set the background image for cell. I have 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM