我想通过 Objective-C 代码在每个单元格右侧的 UITableViewCell 中显示 ImageView 。我使用了以下代码,但它在左侧显示图像。框架未设置。
UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width/2, 0, cell.frame.size.width/2, cell.frame.size.height)];
if([[self.categories objectAtIndex:indexPath.section] isKindOfClass:[SpecialPagesCategory class]]) {
cell.backgroundColor=[UIColor whiteColor];
bgView.image = [UIImage imageNamed:@"fbBtn.png"];
} else {
cell.backgroundColor=[UIColor clearColor];
bgView.image = nil;
}
cell.imageView.image = bgView.image;
cell.imageView.frame = bgView.frame;
如何通过自定义框架重置 UITableViewCell imageView?我正在创建一个如下所示的单元格
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
[self.categoryTable flashScrollIndicators];
cell.textLabel.font=[UIFont fontWithName:OPEN_SANS size:16.0];
cell.backgroundColor=[UIColor clearColor]; }
我也通过Content View试过如下
UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width/2, 0, cell.frame.size.width/2, cell.frame.size.height)];
if([[self.categories objectAtIndex:indexPath.section] isKindOfClass:[SpecialPagesCategory class]]) {
cell.backgroundColor=[UIColor whiteColor];
bgView.image = [UIImage imageNamed:@"fbBtn.png"];
[cell.contentView addSubview: bgView];
} else {
cell.backgroundColor=[UIColor clearColor];
bgView.image = nil;
[cell.contentView addSubview: bgView];
}
“内容 View ”的问题是不满足 if 条件的单元格不会删除 ImageView
最佳答案
创建 ImageView 并将其作为 subview 添加到单元格,它将起作用。
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 50, 50)];// Your specification
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setCornerRadius:5.0f];
[imgView.layer setMasksToBounds:YES];
[imgView setImage:[UIImage imageWithData: imageData]];
[ cell.contentView addSubview:imgView];
关于ios - 如何更改tableCell 的imageView 的frame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32968372/