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");
}
else
{
NSLog (@"cellForRowAtIndexPath:: cell is NOT NIL, successfully dequeue using cell identifier");
}
return cell;
}
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.
Related posts:
- 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
Tags: cellForRowAtIndexPath, dequeueReusableCellWithIdentifier, UITableViewCell

twitter.com/ideveloperdiary
That part in bold about setting the Identifier Field in the xib really helped me out! My custom cells were making the scrolling really slow and I noticed the “cell==nil” was true all the time. That fixed it and the scrolling is smooth now.
Thanks!
What if i don’t have a xib?
I mean I do all programmatically, so I don’t have a xib.
I think this is the reason why my tableView duplicate row every time I populate it
tnx
“you need to set the Identifier field of this view in xib file in Interface Builder to the same identifier string used here – “MyCustomCellIdentifier””
That was it – never realized this. Thank you very much!
i am parsing data to a table view,there are lakhs of names in server page,i have two questions
Firstly i did (with help of php programmer) write all the server data in different pages in server side,and i put a “View more” button under the table view.When user click that,the page number will be increased to one and data on second page is shown to the table view. At that time i am loosing all the first page data on table view. How can i append previous datas to table view when i click “View More”.
If i write all the names in single page in server side, Can i call the data to TableView without memory problems (just like first 10rows and when i scroll down all other data are appending without memory problems)