New View with AdvancedTableViewCell Tutorial

Discussion in 'iOS Programming' started by AreYouAMac, Nov 16, 2010.

  1. AreYouAMac, Nov 16, 2010
    Last edited by a moderator: Nov 16, 2010

    AreYouAMac macrumors regular

    Joined:
    Jan 5, 2008
    #1
    Hey guys,

    I was hoping someone can chime in and give me a view tips on how to do this...

    I'm working with the AdvancedTableViewCells Tutorial, and I created a New View that would show when a user touches a cell.

    i.e.... if they click on "Denmark" then a new view would show saying "Cheerioz!"

    [​IMG]

    [​IMG]

    Everything in the cell is from a plist.

    [​IMG]

    What I would like to do is when a user clicks on the cell, the same data will be passed onto the New View.... i.e. Publisher, Name, Icon will show into the new view.

    The advice that I got was to load the plist into an NSArray using -initWithContentsOfFile: and the path to the file in the bundle.

    Then, in "didSelectRow..." , say the indexPath.row is 1. To access the Publisher of Item 1 (the second item), you would want the second element of the array, and of that element (which is a dictionary), you want the object whose key is "Publisher" .

    I would write this... myLabel.text = [[array objectAtIndex: indexPath.row] objectForKey:mad:"Publisher"];

    _______________________________________________________________

    The coding that I have right now in the "didSelectRow...." (this is in the RootViewController.m)

    Code:
    {
    	NSString *publisher = [[NSBundle mainBundle] pathForResource:@"Appetizer" ofType:@"plist"];
    	
    	DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
    	dvController.publisher = publisher;
    	[self.navigationController pushViewController:dvController animated:YES];
    	[dvController release];
    	dvController = nil;
    }
    
    Question... will I be loading the plist using "initWithContentsOfFile" in the "didSelectRow..."?

    And can anyone give me a tip on how I would write the "myLabel.text...." in the "didSelectRow"???

    Thanks!

    I would greatly appreciate any advice!!!
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I wouldn't! You should already be using the data from the plist "earlier" on, like in cellForRowAtIndexPath:, so you should probably load it even earlier than that, say in viewDidLoad:. Then you can use it throughout your table view controller.

    There are a number of options you have but here's what I would do: You should make properties for the things you want to set in your new view controller and then set them after you instantiate it, very similarly to how you are already setting myLabel.text.
     
  3. AreYouAMac, Nov 16, 2010
    Last edited by a moderator: Nov 16, 2010

    AreYouAMac thread starter macrumors regular

    Joined:
    Jan 5, 2008
    #3
    Thanks!

    Ok, here is what I have in the "viewDidLoad" coding...

    Code:
    - (void)viewDidLoad
    {
    	// Load the appetizer.
        NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"Appetizer" ofType:@"plist"];
        self.appetizer = [NSArray arrayWithContentsOfFile:dataPath];
    }
    
    And for the "cellForRowAtIndexPath"...

    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       
    	// Configure the appetizer for the cell.
    	
        NSDictionary *dataItem = [appetizer objectAtIndex:indexPath.row];
        cell.icon = [UIImage imageNamed:[dataItem objectForKey:@"Icon"]];
        cell.publisher = [dataItem objectForKey:@"Publisher"];
        cell.name = [dataItem objectForKey:@"Name"];
        cell.numRatings = [[dataItem objectForKey:@"NumRatings"] intValue];
        cell.rating = [[dataItem objectForKey:@"Rating"] floatValue];
        //cell.price = [dataItem objectForKey:@"Price"];
    	
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	
        return cell;
    }
    So, if it is already loaded - would I even have to reload it in the "didSelectRow..."?

    Or would I have to rewrite...
    Code:
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"Appetizer" ofType:@"plist"]; or ... 
    NSDictionary *dataItem = [appetizer objectAtIndex:indexPath.row];
    
    Sorry for the newbie questions - I been thinking hard of this for about 3 days and can't seem to figure out how to pass on the information in the cell to the New View.

    Thanks!
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    First, please see this Sticky: Posting code, please use the
    Code:
     tags[/URL]
    
    [quote="AreYouAMac, post: 11435452"]So, if it is already loaded - would I even have to reload it in the "didSelectRow..."?[/QUOTE]
    No, you already have it loaded and have access to it.
    
    [quote="AreYouAMac, post: 11435452"]Or would I have to rewrite... 
    [CODE]NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"Appetizer" ofType:@"plist"]; or ... 
    NSDictionary *dataItem = [appetizer objectAtIndex:indexPath.row];
    
    [/QUOTE]
    No "or" about it. More like "and". Yes, you should rewrite it (or drop some code). dataPath is now an unused variable here.

    Seems you already have an inkling. In earlier code you were doing this:
    Code:
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
    	dvController.publisher = publisher;
    That is basically passing the publisher information to the dvController (assuming you have the property set up correctly).
     
  5. AreYouAMac thread starter macrumors regular

    Joined:
    Jan 5, 2008
    #5
    Thanks!

    It's not an "inkling" per se.... more like "I thought that would have been the correct way to set up one UILabels"... so I know that I did set it up wrong before, because that was not the correct way to show all of the labels. I would need to use
    Code:
    myLabel.text = [[array objectAtIndex: indexPath.row] objectForKey:@"Publisher"];
    for each UILabel / UIImage I make.

    So starting from...
    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    From what I am assuming, since each item is a NSDictionary, would I put the write
    Code:
    NSDictionary *dataItem = [appetizer objectAtIndex:indexPath.row];
    into the
    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    SO the code of this would be...
    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    	NSDictionary *dataItem = [appetizer objectAtIndex:indexPath.row];
    correct?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Well, assuming you're intending on passing the entire NSDictionary to the detail view controller and leaving it up to it to pull out the various values you wish to use. It's hard to say correct because you haven't posted the entire code for didSelectRowAtIndexPath:, nor have we seen any of the code for your DetailViewController class yet.
     
  7. AreYouAMac thread starter macrumors regular

    Joined:
    Jan 5, 2008
    #7
    That's the thing... I don't know what code to write after
    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    	NSDictionary *dataItem = [appetizer objectAtIndex:indexPath.row];
    :( :( :(

    That is all I have in the "-didSelectRow..." besides the necessary coding to push it to the "DetailViewController".

    Here is the rest of the code that I have just to push it through to the New View
    Code:
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
    
    	[self.navigationController pushViewController:dvController animated:YES];
    	[dvController release];
    	dvController = nil;
    }
    But I do not know where or how I would input the
    Code:
    myLabel.text = [[array objectAtIndex: indexPath.row] objectForKey:@"Publisher"];
    
    I feel as if I am very close to pushing through all of the UILabels and UIImage from plist but it's just that I am stuck at this part!
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    This might help.

    That is, what properties do you have setup in your detail view controller to handle the UI elements you are wanting to set?
     
  9. AreYouAMac thread starter macrumors regular

    Joined:
    Jan 5, 2008
    #9
    In the DetailViewController.h

    Here is what I would like to set up...

    Code:
    @interface DetailViewController : UIViewController {
        IBOutlet UIImageView *iconView;
        IBOutlet UILabel *publisherLabel;
        IBOutlet UILabel *nameLabel;
        IBOutlet UILabel *numRatingsLabel;
    	
        UIImage *icon
        NSString *publisher;
        NSString *name;
        NSString *numRatings;
    }
    
    @property (nonatomic, retain) UIImage *icon;
    @property (nonatomic, retain) NSString *publisher;
    @property (nonatomic, retain) NSString *name;
    @property (nonatomic, retain) NSString *numRatings;
    And with that being coded, is this would I would properly code inside of the DetailViewController.m (with everything being synthesized and released) --> just using "publisher" as an example

    Code:
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void) viewDidLoad {
        [super viewDidLoad];
    	
    	publisherLabel.text = publisher;
    }
    With the above being coded in the DetailViewController.h, how would I code this into the RootViewController.h "-didSelectRow"
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    So, you understand this concept:
    Code:
    instanceName.propertyName = value;
    , correct?

    Now, in your -didSelectRow, think about what is your instance, what is your property(s) and what is your value(s)?
     
  11. AreYouAMac thread starter macrumors regular

    Joined:
    Jan 5, 2008
    #11
    Code:
    instanceName.propertyName = value;
    Code:
    publisherLabel.text = publisher;
    So the UILabel (publisherLabel) will show the text (the NSString publisher in the selected row) of the NSString publisher mentioned in the "-didSelectRow..." coded in the RootViewController.m

    In the -didSelectRow in RootViewController.m the property(ies) would be "icon, publisher, name, numRatings" and the value(s) would be the NSString inside of the Appetizer.plist

    Am I correct?
     
  12. AreYouAMac, Nov 16, 2010
    Last edited: Nov 16, 2010

    AreYouAMac thread starter macrumors regular

    Joined:
    Jan 5, 2008
    #12
    THANK YOU!!!!

    YOU HELPED ME FIGURE IT OUT! I AM SOOOOO EXCITED / ECSTATIC!!!!! COUPLE DAYS/WEEKS OVER THIS AND IT FEELS SOOOO GREAT!!!

    Here is the coding if anyone is interested....

    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    	//Get the dictionary of the selected data source.
    	NSDictionary *dictionary = [self.appetizer objectAtIndex:indexPath.row];
    	
    	//Prepare to tableview.
    	DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
        
    	//Set Publisher
    	dvController.publisher = [dictionary objectForKey:@"Publisher"];
    	
    	//Push the new table view on the stack
    	[self.navigationController pushViewController:dvController animated:YES];
    	
    	[dvController release];
    	dvController = nil;
    }
    THANKS AGAIN!

    Now, I hope it will be the same for way to set up the image.... ; )
     
  13. AreYouAMac thread starter macrumors regular

    Joined:
    Jan 5, 2008
    #13
    EDIT - FOUND FIX - HERE IS THE THE NEW CODE THAT SHOULD BE USED...

    Code:
    //Set Icon
    dvController.icon = [UIImage imageNamed:[dataItem objectForKey:@"Icon"]];
    Wadda know? When I try to load the iconView, the app closes... here is my coding...

    Code:
    DetailViewController.h
     
    @interface DetailViewController : UIViewController {
        IBOutlet UIImageView *iconView;
        IBOutlet UILabel *publisherLabel;
        IBOutlet UILabel *nameLabel;
     
        UIImage *icon;
        NSString *publisher;
        NSString *name;
    }
     
    @property (nonatomic, retain) UIImage *icon;
    @property (nonatomic, retain) NSString *publisher;
    @property (nonatomic, retain) NSString *name;
     
    @end
    Code:
    DetailViewController.m (everything is synthesized and released)
     
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void) viewDidLoad {
        [super viewDidLoad];
     
    iconView.image = icon;
    nameLabel.text = name;
    publisherLabel.text = publisher;
    }
    Code:
    RootViewController.m
     
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    //Get the dictionary of the selected data source.
    NSDictionary *dictionary = [self.appetizer objectAtIndex:indexPath.row];
     
    //Prepare to tableview.
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
     
    //Set Icon
    dvController.icon = [dictionary objectForKey:@"Icon"];
     
    //Set Name
    dvController.name = [dictionary objectForKey:@"Name"];
     
    //Set Publisher
    dvController.publisher = [dictionary objectForKey:@"Publisher"];
     
    //Push the new table view on the stack
    [self.navigationController pushViewController:dvController animated:YES];
     
    [dvController release];
    dvController = nil;
    }
    But both Name and Publisher showed fine in the New View, but when I try to implement the icon, the app closes on me.

    Can anyone chime in?

    Thanks!
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    What type of object does [dictionary objectForKey:mad:"Icon"] return? Can you assign it to a UIImage?
     
  15. AreYouAMac thread starter macrumors regular

    Joined:
    Jan 5, 2008
    #15
    Hey!

    Thanks I found the fix...
    Code:
    //Set Icon
    dvController.icon = [[B]UIImage imageNamed:[[/B]dataItem objectForKey:@"Icon"][B]][/B];
     

Share This Page