在UITableView中的单元格中添加多个图像

时间:2011-12-10 09:16:46

标签: objective-c image uitableview

如何在UITableView

的单个单元格中添加多个图像

我想用图像来表示玩家的状态,每个玩家可能同时拥有多种状态。

1 个答案:

答案 0 :(得分:6)

继承UITableViewCell以制作自定义单元格。让我们说它叫MyCustomCell

使用它如下

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    // In your case it has to be the custom cell object here.

    MyCustomCell *cell = (MyCustomCell*)[tableView dequeueReusableCellWithIdentifier:@"AnIdentifierString"];

    if (cell == nil) 
    {
        cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"AnIdentifierString"] autorelease];
    }

    //use your cell here
    //  cell.textLabel.text = @"This text will appear in the cell";
    return cell;
}

对于多个图像,您可以使用MyCustomCell样式覆盖init,如下所示

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

    //change frame sizes to palce the image in the cell
        UIImageView * thumbnail1 = [[UIImageView alloc] initWithFrame:CGRectMake(17, 12, 62, 62)];
        thumbnail1.image = [UIImage imageNamed:@"Icon.png"];
        [self addSubview:thumbnail1];

        UIImageView * thumbnail2 = [[UIImageView alloc] initWithFrame:CGRectMake(17, 12, 62, 62)];
        thumbnail2.image = [UIImage imageNamed:@"Icon.png"];
        [self addSubview:thumbnail2];

        UIImageView * thumbnail3 = [[UIImageView alloc] initWithFrame:CGRectMake(17, 12, 62, 62)];
        thumbnail3.image = [UIImage imageNamed:@"Icon.png"];
        [self addSubview:thumbnail3];

    }
    return self;
}