Changing bounds of imageView of UITableViewCell(改变 UITableViewCell 的 imageView 的边界)
问题描述
我正在尝试在 UITableViewCell 的 imageView 中放置各种尺寸的图像.我异步获取图像数据,创建图像,设置 imageView 的内容模式,最后设置 imageView 的边界.但是代码似乎对我所做的任何更改都不敏感.我希望图像以 75x75 区域为中心.为此,我编写了以下代码
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)];[持有人 setNeedsLayout];
其中 holder 是 UITableViewCell.我得到的结果总是一样的.所有图像都有 75 像素的高度和不同的宽度.有人可以帮我解决这个问题吗?
我意识到设置 contentMode 和 bounds 属性对该代码没有任何影响.我在最后一行之后添加了一个 NSLog,得到的结果如下:
NSLog(@"imageview:%@ bounds and contentMode:%@ %@",[holder imageView],[holder.imageView bounds],[holder.imageView contentMode]);
<块引用>
imageview:
还是没有办法
完成了,我终于找到了解决方案,不过花了我 3 个小时 =)
解决方案是在自定义 UITableViewCell 类的 -(void)layoutSubviews 方法中更改 bound,frame,contentMode 等属性.诀窍"就是在这种方法中编写布局代码,否则代码没有任何效果.
以下代码为我完成了工作.它使表格的行垂直对齐.
- (void)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;}
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];
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?
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]);
imageview:<UIImageView: 0x39ab8a0; frame = (0 0; 75 75); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x39a92b0>> bounds and contentMode:(null) (null)
Still no solution
Done, I finally found the solution, it cost me 3 hours though =)
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 的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:改变 UITableViewCell 的 imageView 的边界
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01