Nein Danke
Nein Danke

Reputation: 107

Swift: Best way to add one single image to all cells in a UITableView

I have a large image for the background of table cells (to support all devices and orientations). Since I want to save memory I thought it would be a good idea to make an UIImage with the correct dimensions in the ViewDidLoad:

let bgImage = UIImage(named: "cellBG_001.png")
var bgView = UIImageView()
bgView = UIImageView(image: bgImage!)
bgView.frame = CGRect(x: 0, y: 0, width: cellWidth, height: cellHeight)

and then re-use this for every cell. Then I learned that a view can only have one parent view and ended with the same code, but in the cellForRowAtIndexPath-method. Now I feel bad about that ; ) is there a better way to do this?

Upvotes: 1

Views: 220

Answers (1)

Stefan Salatic
Stefan Salatic

Reputation: 4523

You should just create one UIImage object and then assign it to all UIImageViews in your cells. Also since UITableView is reusing cells you shouldn't have problems with performance if you are dequeueing cells.

Upvotes: 2

Related Questions