将UIImageView添加到自定义UITableView Cell

时间:2011-12-08 11:10:50

标签: ios xcode uitableview

这是一种正确的方法还是有更好的解决方案?

非常感谢!

Code(rowAtIndexPath)

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(500, 50, 20, 20)];
        imageView.image = [UIImage imageNamed:@"Icon.png"];
        [cell addSubview:imageView];

4 个答案:

答案 0 :(得分:4)

您应该将视图添加到单元格的contentView而不是单元格的视图中。这是一个例子:

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(500, 50, 20, 20)];
imageView.image = [UIImage imageNamed:@"Icon.png"];
[cell.contentView addSubview:imageView];

答案 1 :(得分:1)

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]; 

这是正确的。 对于UITableView的大型列表。 如果您使用[cell addSubview:],对于大型列表,它看起来就像闪烁一样。

答案 2 :(得分:0)

有我的代码,它运作良好。希望它可以帮助你。

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];

答案 3 :(得分:0)

对于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)