Since the iPhone is a mobile device it is limited in its memory. This needs to be taken into account when programming apps for the iPhone, especially if you intend to distribute the app on the App Store. You then wants to have the best user experience, a quick and fast loading application.
Since a table view is used very often to display information in iPhone apps, I would like to devote a few words on a bug I usually had seen in my applications, especially when I just started writing my first few apps. It would end me with a relatively slow loading table view and undesired use of valuable memory.
The function below is used in a UITableView class to allow you to return the cell for your Table View:
What you would usually do is create your class named MyCustomCell of type UITableViewCell with a XIB file.
You would then edit your view in the XIB file visually using Interface Builder to customize your own table cell feel and look.
Below is how you would return the cell object from the function:
static NSString *CellIdentifier = @"MyCustomCellIdentifier"; // we need to set this in nib as identifier of the cell for this to work!
MyCustomCell *cell = (MyCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog (@"cellForRowAtIndexPath:: cell is nil, loaded from MyCustomCell.xib file");
NSLog (@"cellForRowAtIndexPath:: cell is NOT NIL, successfully dequeue using cell identifier");
In a call to cellForRowAtIndexPath you want to create a unique string identifier to identify this cell view (I called it here – “MyCustomCellIdentifier”) and try to dequeue a Reusable Cell With Identifier as soon as the function starts. If there is no reusable cell (there will not be on first cell creation calls made to this function) we will use loadNibNamed to load our cell view from nib file by specifying the xib filename, “MyCustomCell” in this case.
Now pay real attention to this: I had missed this a couple of times – you need to set the Identifier field of this view in xib file in Interface Builder to the same identifier string used here – “MyCustomCellIdentifier”, otherwise when view is loaded from xib file we will not queue it by the correct identifier and on subsequent calls to this function we will also not be able to dequeueReusableCellWithIdentifier successfully. This will cause the “if (cell == nil)” to be true and instead of a quick dequeue of a cell we will have to load the cell view from the xib file, losing time and being inefficient.
Make sure that after you code this in your project you place NSLog statments like I have and making sure that only to the first few cells (actually those first rows occupying the initial screen size before you started scrolling down) are triggering a print of “cellForRowAtIndexPath:: cell is nil….” and the next rows are triggering a print of “cellForRowAtIndexPath:: cell is NOT NIL, successfully dequeue using cell identifier“.
This will make sure you are efficient and that your table view loads as fast as possible.
- Creating custom UITableViewCell – a common practice in many iPhone apps
- Supporting iPhone Landscape/Portrait orientations in UITableViewCells
- Adding a UISearchBar to the top part of your table view