2011-12-08 65 views
1

这是一种合适的方式还是有更好的解决方案?将UIImageView添加到自定义UITableView Cell

非常感谢!

Code(rowAtIndexPath)

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

[http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html](http://developer。 apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html):) – Kjuly

+0

我只是使用[cell addSubview:imageView]。但对于其余的它看起来不错 – Novarg

回答

4

您应该添加您的意见,细胞的内容查看没有电池的观点。这里有一个例子:

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

我试过了。我在IB中创建了一个没有contentView插座的原型单元。 (不要问我为什么......)使用原型的自定义.h文件和自定义图像视图的IBConnection解决了问题。我现在正在使用自定义按钮(无论如何),因为我决定用图像捕捉触摸事件。 – DAS

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]; 
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:]它将看起来像闪烁大列表。

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)