Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 10, 2012, 11:11 PM   #1
daproject85
macrumors member
 
Join Date: Apr 2011
cellForRowAtIndexPath method question

Hi Forum,

can someone please explain to me that if i have this code

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Top Places Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
    }
so let me see if i even know what the heck i am talking about. so the "dequeueReusableCellWithIdentifier" method is purely for performance.
basically we are saying if the cell exists and its off screen just re-use it when I need to draw another one. So if we dont have any off screen.. lets say my table only has 5 rows which all 5 fit on the screen. At that point there is nothing to "dequeue" so i need to create new cells. Hence the IF statement Makes sense. But i ran the exact same code with the if statement commented out, AND IT STILL RUNS!..... i am so confused Please shed some light on this.
daproject85 is offline   0 Reply With Quote
Old Sep 11, 2012, 12:25 AM   #2
KarlJay
macrumors 6502a
 
Join Date: May 2010
Location: California
I'll take a stab at this:
The program will work the same with or without cell reuse. Cell reuse is a way to conserve memory. If you have a small number of rows and don't scroll a lot, it won't really matter much.

If you have a large number or rows and don't reuse cells, you are using memory that the program might need.

The program will function the same.

As a "rule of thumb" it's best to have the cell reuse because you might end up loading more rows than you once though.

Let me know if this helps or not.
KarlJay is offline   0 Reply With Quote
Old Sep 11, 2012, 01:06 AM   #3
Scott90
macrumors 6502
 
Join Date: Jul 2008
Creating a cell using alloc/init is not necessary anymore with storyboards, as you'll have a prototype cell with that reuse identifier. This means that a call to dequeueReusableCellWithIdentifier always returns a cell (provided that the identifier is correct), and the if statement is never executed anyway.

darealjakesh is correct. A table view will only put a few cells in memory, just enough to be able to show a full screen of cells. Once scrolled off-screen, a cell stays in memory, but will go to the reuse stack. It can now be reused and shown at the bottom of the visible portion of the table.
Scott90 is offline   0 Reply With Quote
Old Sep 11, 2012, 01:23 AM   #4
MattInOz
macrumors 68030
 
MattInOz's Avatar
 
Join Date: Jan 2006
Location: Sydney
-dequeueReusableCellWithIdentifier has been upgraded over the last few versions to use a a bunch of optimizations. As scott said if your using a storyboard you can set the Reuse ID there. If your using a NIB or a custom UITableView Class you can register them against a reuse ID and avoid a bunch of spaghetti code in your cellForRowAtIndexpath method.
__________________
There is no such thing as "Collective Wisdom"
[ iPhone 5s, iPad Mini, 13" MacBookPro 2.7Ghz, 27"Al iMac i7, Black MacBook 13"]
MattInOz is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Simple question about making / releasing an object within a method Ja Di ksw Mac Programming 5 Oct 16, 2013 05:28 AM
Class Method -> Function -> Instance Method? Blakeasd Mac Programming 11 Jun 19, 2013 03:53 AM
Best method for using 2 HD? OmarKanawati OS X 10.8 Mountain Lion 10 Jun 14, 2013 09:51 AM
Why would I get nil for [self.tableView cellForRowAtIndexPath:indexPath]? Ron C iPhone/iPad Programming 20 Jun 8, 2012 07:56 PM

Forum Jump

All times are GMT -5. The time now is 09:12 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC