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

    Joined:
    Dec 19, 2011
    #1
    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.
     
  2. jonnymo5 macrumors 6502

    Joined:
    Jan 21, 2008
    Location:
    Texas
    #2
    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/
     
  3. ArtOfWarfare, Jan 1, 2012
    Last edited: Jan 1, 2012

    ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    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.
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    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