简体   繁体   English

UIImageView setFrame:在UITableViewCell中不起作用

[英]UIImageView setFrame: not working in UITableViewCell

I've an UIImageView in customized UITableViewCell and I want to resize it, I set it view mode to LEFT. 我在自定义的UITableViewCell有一个UIImageView ,我想调整它的大小,将其查看模式设置为LEFT。 To resize I'm applying below code. 要调整大小,我正在应用以下代码。

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

My image width is 100 and I want to reduce is to 50 but it still shows me in 100 width. 我的图像宽度是100,我想减小到50,但仍然显示100宽度。 If I set view mode to Scale To Fill it works fine but I want to have it's view mode to LEFT. 如果我将视图模式设置为“ Scale To Fill则效果很好,但我希望将其视图模式设置为“左”。

Full method cellForRowAtIndexPath is as given below: 完整的方法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;
}

You are doing to resize the imageView frame not the image so You can crop your image as you want, 您正在调整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);

set IBImage frame in DetailCell class in 在DetailCell类中设置IBImage框架

-(void)layoutSubviews:

method 方法

Add the following lines to the File's owner to make sure it knows how to respond to framechanges. 将以下行添加到文件的所有者,以确保它知道如何响应框架更改。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM