问题描述
我正在尝试将各种大小的图像放在UITableViewCell的imageView中。我得到图像数据异步,创建图像,设置imageView的内容模式,最后设置imageView的边界。但是代码似乎对我所做的任何更改都不敏感。我希望图像以75x75的区域为中心。我为此目的编写了以下代码
I'm trying to place various size images inside imageView of UITableViewCell. I get the image data asynch'ly, create the image, set the content mode of imageView and finally set bounds of imageView. But the code seems insensitive to any changes I made. I want the images to be centered in a 75x75 area. I wrote the below code for this purpose
UIImage* image = [UIImage imageWithData:data];
[holder.imageView setContentMode:UIViewContentModeCenter || UIViewContentModeRedraw];
[holder.imageView setImage:image];
[holder.imageView setBounds:CGRectMake(0,0,75,75)];
[holder.imageView setFrame:CGRectMake(0,0,75,75)];
[holder setNeedsLayout];
持有者是UITableViewCell。我得到的结果总是一样的。所有图像的高度均为75像素,宽度不同。有人可以帮我解决这个问题吗?
Where holder is the UITableViewCell. The result I get is always the same. All images have 75px height and different widths. Can someone help me solve this problem?
我已经意识到设置contentMode和bounds属性对该代码没有任何影响。我在最后一行之后添加了一个NSLog,结果如下:
I have realized that setting contentMode and bounds properties does not have any effect in that code. I have added an NSLog after the last line and got the results as below:
NSLog(@"imageview:%@ bounds and contentMode:%@ %@",[holder imageView],[holder.imageView bounds],[holder.imageView contentMode]);
仍然没有解决方案
推荐答案
完成后,我终于找到了解决方案,虽然我花了3个小时=)
Done, I finally found the solution, it cost me 3 hours though =)
解决方案是更改自定义UITableViewCell类的 - (void)layoutSubviews方法中的bound,frame,contentMode等属性。 技巧是在此方法中编写布局代码,否则代码没有任何影响。
The solution is to change properties like bound,frame,contentMode in -(void)layoutSubviews method of the custom UITableViewCell class. The "trick" is to write layout code in this method, otherwise the code does not have any effect.
下面的代码为我做了工作。它使表格的行垂直对齐。
Below code did the work for me. It makes rows of the table vertically aligned.
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.bounds = CGRectMake(0,0,75,75);
self.imageView.frame = CGRectMake(0,0,75,75);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
CGRect tmpFrame = self.textLabel.frame;
tmpFrame.origin.x = 77;
self.textLabel.frame = tmpFrame;
tmpFrame = self.detailTextLabel.frame;
tmpFrame.origin.x = 77;
self.detailTextLabel.frame = tmpFrame;
}
这篇关于更改UITableViewCell的imageView边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!