简体   繁体   English

如何在iPhone中的表格视图单元格中设置背景图像

[英]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) (虽未测试)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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