ios - 如何更改tableCell 的imageView 的frame?

标签 ios objective-c uitableview layout

我想通过 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/

相关文章:

ios - 关于 UIScrollView 和更改 UIView 所有权的一些帮助

iphone - 使用自动释放返回对象,但我仍然泄漏内存

ios - 展开和折叠部分

ios - 在 AVFoundation 或 MPMoviePlayerViewController ios 中快速浏览视频

ios - iOS 上未调用 GMSMapViewDelegate 函数

ios - 由于 UILongPress,当我触摸它时 UIButton 未被识别

objective-c - 查找 NSArray 中所有特定长度的字符串

ios - UITableViewCell 内的 UITableView - didSelectRowAtIndexPath

ios - Swift:UIView 出现在 UITableView 单元格中

ios - 为 UITableViewCell 的 NSTextAttachment 异步加载图像