iOS question about the cellForRowAtIndexPath

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
Im working on to customize my cell like below
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
1. static NSString *CellIdentifier = @"reusedCell";    
2. DetailCell *cell    = (DetailCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Customize the cell of each row from table
3. if ( cell == nil ) {
4.     NSLog(@" MY CELL IS NIL");
5.     cell    =   [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier];
6.}
7. else
8.     NSLog(@" MY CELL IS NOT NIL");

}
By debugging, I know that line 4-5 are not reached at all
Code:
MY CELL IS NOT NIL
Therefore, I cant create my own cell
According to apple documentation
Code:
Typically, the first thing the data source should do is send dequeueReusableCellWithIdentifier: to the table view, passing in a reuse-identifier string. [B]If the table view does not return a reusable cell object, the data source creates one, assigning the object a reuse identifier in the final parameter of initWithStyle:reuseIdentifier:[/B]
Therefore, in my situation, the table view does return a reusable cell object... Where it comes from then...
Have any one experienced this issue before. Please help thanks.
 
Last edited:

jonnymo5

macrumors 6502
Jan 21, 2008
279
0
Texas
Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3)

This tutorial might help.
http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,698
4,319
Since your cell isn't nil, start setting it up.

Outside of your if and else, have something like
Code:
cell.titleLabel.text = @"My Cell";
Conceptually, the way cells work is...

Look for a cell with a certain cell identifier that isn't on screen but is allocated in memory.
If one isn't available, make one.
Once one was either found or made, take it and set it up.
 
Last edited:

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
Therefore, I cant create my own cell
This is false though.
since this is a little nifty machine with not that much memory spare for apps, you get a limit. The thing Apple does is indeed dequeing cells (or not), to use some memory to create new ones, or copy them with new stuff on it.
Basically, if you 5 cells on your screen, it will create 5..
If you have more, it won't load the other cells yet, but will only do so, once you start scrolling, if you are dequeing, it will reuse some memory from the former created ones to make new ones, and otherwise, just create some new ones on the fly while scrolling.
Apple totally decides this, if it feels like creating new cells instead dequeing them, then it will do so.. so you will need to know ;p

Greetz, Noxx
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.