【问题标题】:UIImageView setFrame: not working in UITableViewCellUIImageView setFrame:在 UITableViewCell 中不起作用
【发布时间】:2012-12-15 07:25:38
【问题描述】:

我有一个自定义UITableViewCell 中的UIImageView,我想调整它的大小,我将它的查看模式设置为左。要调整大小,我正在应用以下代码。

[cell.IBImage setFrame:CGRectMake(201,12,50,20)];

我的图像宽度是 100,我想减小到 50,但它仍然显示为 100 宽度。如果我将查看模式设置为Scale To Fill,它可以正常工作,但我希望将其查看模式设置为 LEFT。

完整的方法 cellForRowAtIndexPath 如下:

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

static NSString *CellIdentifier = @"DetailCell";

DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DetailCell" owner:self options:nil];

cell = (DetailCell *)[topLevelObjects objectAtIndex:0];

clsDetailResponse *Response = [self.Histories objectAtIndex:[indexPath row]];

[cell.IBUsernameLabel setText:[NSString stringWithFormat:@"%@ %@", Response.Firstname, Response.Lastname]];

[cell.IBImage setFrame:CGRectMake(201, 12, 50, 20)];

return cell;
}

【问题讨论】:

  • 你在哪里辞职..在延迟加载技术中加载图像后
  • @Rajneesh071 抱歉,我没有收到您的问题...
  • 你在哪里调整它的大小..请提供更多代码
  • @Rajneesh071 我正在调整它的大小 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ,如果我将视图模式设置为 Scale To Fill 问题,它工作正常仅适用于左视图模式
  • 可以粘贴cellforrow的代码吗

标签: iphone objective-c ios uiimageview uiimage


【解决方案1】:

您正在调整 imageView 框架而不是图像的大小,以便您可以根据需要裁剪图像,

UIImage *ima = cell.image.image;
CGRect cropRect = CGRectMake(0, 0, 50,20);
CGImageRef imageRef = CGImageCreateWithImageInRect([ima CGImage], cropRect);
[cell.image setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);
cell.image.frame=CGRectMake(10, 10, 50, 20);

【讨论】:

    【解决方案2】:

    在 DetailCell 类中设置 IBImage 帧

    -(void)layoutSubviews:
    

    方法

    【讨论】:

    • 这是不可能的,因为宽度是动态的,并且在方法 cellForRow... 方法中决定
    • 将动态宽度传递给 DetailCell 类,并在layOutSubviews 方法中使用传递的参数。
    • DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell.IBImageWidth = 你的宽度;在 DetailCell 类的 layoutSubViews 方法中,[IBImage setFrame:CGRectMake(x,y,IBImageWidth,height)],其中 IBImageWidth 是 detailCell 类中的 int 类型的属性
    • 谢谢 Vishal,但它仍然无法正常工作......问题在于图像查看模式 LEFT 否则它正在工作
    • 图像查看模式向左表示??你的 imageview 的宽度现在正在改变吗?
    【解决方案3】:

    将以下行添加到文件的所有者,以确保它知道如何响应帧更改。

        [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
        [self setClipsToBounds:YES];
        [self setAutoresizesSubviews:YES];
    

    【讨论】:

    • 能否详细解释一下?
    • DetailCell 类的initWithNibawakeFromNib 方法中添加这三行,具体取决于结构。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    相关资源
    最近更新 更多