UIImageView在UITableViewCell中不可见
问题描述:
我在UITableView Cell中添加了UIImageView,但它不可见。 但是,当你选择了行,图像是可见的,它是如此困惑。 你能帮助我吗?UIImageView在UITableViewCell中不可见
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier: (NSString *) reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"one.png"]];
cellImage.frame = CGRectMake(240, 0, 20, 20);
[self.contentView addSubview:cellImage];
self.textLabel.text = @"parent";
}
return self;
}
答
你应该只添加cellImage
为accessoryView
。
[cell setAccessoryView: cellImage];
答
你应该首先初始化您的ImageView,并设置其框架 那么你应该设置其image.See给定的代码为CustomtableViewCell
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(3, 3, 80, 80)];
imgView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:imgView];
现在在你的控制器的tableViewCellforrowatindexpath方法 的UE给定的代码
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.imgView.image = [UIImage imageNamed:@"123.jpg"];
这将在乌尔imgView添加图像
它的工作原理,谢谢。 – haibarahu