将图像添加到自定义单元子视图

时间:2011-08-19 15:49:46

标签: iphone objective-c ios cocoa-touch

的cellForRowAtIndexPath:

  CGRect nameValueRect = CGRectMake(80, 5, 200, 15);
            UILabel *nameValue = [[UILabel alloc] initWithFrame:
                                  nameValueRect];
            nameValue.tag = kNameValueTag;
            [cell.contentView addSubview:nameValue];
            [nameValue release];

...

 NSUInteger row = [indexPath row];
    NSDictionary *rowData = [self.computers objectAtIndex:row];
    UILabel *name = (UILabel *)[cell.contentView viewWithTag:
                                kNameValueTag];
    name.text = [rowData objectForKey:@"Name"];

此代码来自我正在使用的教程,该教程在单元格中创建子视图以添加一些文本。

但是,我想要做的不是添加文字而是放置图像。

我试过UIImageView * posterValue = [[UIImageView alloc] initWith ...哦,UIImageView没有initWithFrame。

也许,有人可以解释图像的过程。我甚至需要为UIImageView设置帧大小吗?我需要定位它。

编辑:

我的新代码:

UIImageView *posterValue = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,105,175)];
        posterValue.tag = kPosterValueTag;
        [cell.contentView addSubView:posterValue];
        [posterValue release];

addSubView不适用于UIImageView。说UIView可能不响应addSubView

1 个答案:

答案 0 :(得分:2)

这是addSubview而不是addSubView - 案例很重要。