This question is related to best practice when displaying a table view, so if there is a better way to solve this problem than my proposed solution (in the question) please tell me.
I have a table view which has a text which is contained in an NSArray of Dictionaries as the source for each cell text. The table view should also display a UIImage in the table cell. Every cell will have a text, but some cells may not contain UIImage.
What is the best approach to this problem?
Currently, if a UIImage is available for the text it is set as a key in the Dictionary which also contains the text and displayed. In terms of memory management is this a bad approach?
Alternatively, does it make sense to use two NSArrays which correspond to each other (e.g. they both have the same number of elements and the object at index 1 of the first array would correspond to the object at index 1 of the second array) and just save a NSNull or something similar in the second (image) array which might not always have an object which needs to be displayed?
That is all, thanks for any input!
Either method works, although the NSArray of NSDictionarys would usually be a little more clear for others reading your code (or for yourself months down the line). Even better might be an array of custom NSObject subclass objects to hold the text and image explicitly, although the memory savings are probably minor.
As for storing the UIImages, memory concerns are the same whether they are in a dictionary or in a separate array. If you’re creating them with
imageWithContentsOfFile:orinitWithContentsOfFile:, you should be pretty good as UIImage should automatically free the image data when necessary (and the image data is not otherwise in use) and then reload it from the file when it is needed again. If you’re creating your UIImages in some other manner and they are large or especially numerous, you may want to look into saving the data to files of some sort so you can useimageWithContentsOfFile:.