Beginner Question About Pointers

Discussion in 'iOS Programming' started by Trickerie, Jun 23, 2010.

  1. Trickerie macrumors newbie

    Joined:
    Jun 23, 2010
    #1
    Ok, so, I'm just beginning my study into iPhone programming, and had a pretty basic question.

    Heres a section of code using table views I just can't get my head around:

    Code:
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
    NSString *name = [[contacts objectAtIndex: indexPath.row] valueForKey: @"Name"];
    	
    UILabel *label = [cell textLabel];
    	
    label.text = name;
    
    
    My question may be pretty basic, but why exactly does that change cell's text?

    Any help would be great! Thank you!
     
  2. Alkiera macrumors regular

    Joined:
    Mar 11, 2008
    #2
    Well, it gets a reference to the cell, and then the cell's label, and the label.text = name is the same as [label setValue:name forKey:mad:"text"]... which is why the cell changes. Seems like a lot of effort to update a cell value, but I'm pretty new to Cocoa myself.
     
  3. Trickerie thread starter macrumors newbie

    Joined:
    Jun 23, 2010
    #3
    That was what I was thinking myself, but I just wanted to be sure.

    The long-way-around way of changing a cell is probably done because the sample code is from a book I'm reading. Its actually a really good book, though, for beginners!

    Its called:

    iPhone for Programmers: An App Driven Approach, from the Deitel Developer Series.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    It could just as easily be written:
    Code:
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
    NSString *name = [[contacts objectAtIndex: indexPath.row] valueForKey: @"Name"];
    	
    cell.textLabel.text = name;
    
    The reason for keeping the cell reference in a variable is that it is commonly needed for other things within the method, like instantiating a cell if one wasn't able to be dequeued. As for name, having a reference to it allows you to check, via debugging, if it was set properly before using it in the cell.
     
  5. Trickerie thread starter macrumors newbie

    Joined:
    Jun 23, 2010

Share This Page