xcode 将 UIImageView 添加到自定义 UITableView 单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8430034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding UIImageView to custom UITableView Cell
提问by DAS
Is this a proper way or are there better solutions?
这是一种正确的方法还是有更好的解决方案?
Thanks a lot!
非常感谢!
Code
(rowAtIndexPath)
Code
(rowAtIndexPath)
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(500, 50, 20, 20)];
imageView.image = [UIImage imageNamed:@"Icon.png"];
[cell addSubview:imageView];
回答by RaffAl
You should add your views to cell's contentView not cell's view. Here's an example:
您应该将您的视图添加到单元格的 contentView 而不是单元格的视图。下面是一个例子:
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(500, 50, 20, 20)];
imageView.image = [UIImage imageNamed:@"Icon.png"];
[cell.contentView addSubview:imageView];
回答by Vinod Chaudhari
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.size.height, 20, 20)];
imageView.image = [UIImage imageNamed:@"Icon.png"];
[cell.contentView addSubview:imageView];
It is correct one.
for large list of UITableView.
If you use [cell addSubview:]
it will look like flickering for large list.
它是正确的。用于 UITableView 的大列表。如果您使用[cell addSubview:]
它,对于大列表,它看起来会闪烁。
回答by Farheen Banu
For Swift 3
对于 Swift 3
let imgView : UIImageView = UIImageView(frame: CGRect(x: 500, y: 50, width: 70, height: 70))
//CALayer for rounded image
let imageCALayer : CALayer = imgView.layer
imageCALayer.cornerRadius = 35
imageCALayer.masksToBounds = true
imgView.image = UIImage(string: "Icon.png")
cell.contentView.addSubview(imgView)
回答by Steve
There is my code and it work well.Hope it helps you.
有我的代码,它运行良好。希望对您有所帮助。
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.size.height, 20, 20)];
imageView.image = [UIImage imageNamed:@"Icon.png"];
[cell.contentView addSubview:imageView];