I create most of my UITableView cells from nibs. On this particular project there is a lot and they are of a variety of sizes.
Now I specify my cell height in my nib when I’m designing it, and I want this to be the only place I specify this value.
However, tableView:heightForRowAtIndexPath:, is called before my nib is loaded in cellForRowAtIndexPath, so I don’t have the value.
This is ‘mildly’ annoying, as I have to specify my height again in the code.
I could preload a nib beforehand to get this value. Has anyone got any clever patterns they use to manage this, or do you just live with it?
Well, I can think of three ways to make this a bit easier:
1) encode the height into the nib name – myNib.47.xib
2) Create a mutable dictionary, and at viewDidLoad create entries for the reuseIdentifier as the key and a nsnumber as the object.
3) Variation on 2. At viewDidLoad, associate UINib objects with the reuseIdentifier using:
in a mutable dictionary. When you need a cell, you call:
You can then instantiate a cell when you need the height, then toss it. The instantiate is a lot faster than going to the file system to read the file on each access.