question about the cellForRowAtIndexPath

Discussion in 'iOS Programming' started by tranvutuan, Jan 1, 2012.

  1. tranvutuan, Jan 1, 2012
    Last edited: Jan 1, 2012

    tranvutuan macrumors member

    Dec 19, 2011
    Im working on to customize my cell like below
    - (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 
    7. else
    8.     NSLog(@" MY CELL IS NOT NIL");
    By debugging, I know that line 4-5 are not reached at all
    Therefore, I cant create my own cell
    According to apple documentation
    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.
  2. jonnymo5 macrumors 6502

    Jan 21, 2008
    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.
  3. ArtOfWarfare, Jan 1, 2012
    Last edited: Jan 1, 2012

    ArtOfWarfare macrumors G3


    Nov 26, 2007
    Since your cell isn't nil, start setting it up.

    Outside of your if and else, have something like
    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.
  4. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    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

Share This Page