views:

123

answers:

3

Im having major problems with Detail views and was hoping someone could point me in the right direction.

I would like to add a detail view to an app like apples advancetableviewcells. So once you click on a cell, it leads you to the detail view.

Only catch is, it has to load the data from the cell into the detail view also.

I've looked at allot of sites and allot of examples online, and I can see how it done with simple table but when it comes to individual cells and the data in the .plist. I'm lost.

Please any help would be great. Thanks

A: 

Hi! Adding a detail controller isn't that difficult to implement. In your root view - I guess - you collect all the data from your plist file you mentioned above and order the data after your own criterias. Why don't you create a NSArray in the root view controller which contains a NSDictionary for every cell. In that dictionary you put all the information like title, price or something else of that object. In your detail controller just add an NSDictionary property. If you select a cell, the didSelectRowAtIndexPath method is called. In this method you set the NSDictionary property in the detail view controller as the object in the NSArray of your root view controller at the index indexPath.row

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *myDetailController = [[DetailViewController alloc] initWithNibName:@"NibBame" bundle:nil];
    myDetailController.detailDict = [rootViewArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:myDetailController animated:YES];
    [myDetailController release];
}

Afterwards all the data for the specific cell is now available in the detail view controller, and you can use the advantages of the NSDictionary and can get the variables just by a simple key. For example:

- (void)viewDidLoad {
    self.title = [myDictionary valueForKey:@"specificKey"];
}
burki
I am still having a couple of porblems with this, i tried to paste the code on the reply to your comment but i think i went over in characters, so added it as a reply to the question.Thank you.
A: 

I have tried to follow your examples but still am having trouble. How far off am I with this?

my DetailViewController.h

 #import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "ApplicationCell.h"



@interface DetailViewController : UIViewController {
    IBOutlet UIImageView *iconView;
    IBOutlet UILabel *publisherLabel;
    IBOutlet UILabel *nameLabel;
    IBOutlet UILabel *priceLabel;

}




@end

my DetailViewController.m

    #import "DetailViewController.h"


@implementation DetailViewController

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = [NSArray valueForKey:@"Publisher"];
    self.title = [NSArray valueForKey:@"Icon"];
    self.title = [NSArray valueForKey:@"Name"];

    self.navigationItem.title = @"Detail View";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}



- (void)setIcon:(UIImage *)newIcon
{
    [super setIcon:newIcon];
    iconView.image = newIcon;
}

- (void)setPublisher:(NSString *)newPublisher
{
    [super setPublisher:newPublisher];
    publisherLabel.text = newPublisher;
}


- (void)setName:(NSString *)newName
{
    [super setName:newName];
    nameLabel.text = newName;
}



- (void)dealloc
{
    [iconView release];
    [publisherLabel release];
    [nameLabel release];
    [priceLabel release];

    [super dealloc];
}


@end

and my rootviewcontroller.m

#import "RootViewControllerPoints.h"
#import "DetailViewController.h"




#define USE_INDIVIDUAL_SUBVIEWS_CELL    1

#define DARK_BACKGROUND  [UIColor colorWithRed:151.0/255.0 green:152.0/255.0 blue:155.0/255.0 alpha:1.0]
#define LIGHT_BACKGROUND [UIColor colorWithRed:172.0/255.0 green:173.0/255.0 blue:175.0/255.0 alpha:1.0]


@implementation RootViewController

@synthesize tmpCell, data;


#pragma mark View controller methods

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the table view.
    self.tableView.rowHeight = 73.0;
    self.tableView.backgroundColor = DARK_BACKGROUND;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    // Load the data.
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    self.data = [NSArray arrayWithContentsOfFile:dataPath];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            return YES;
        default:
            return NO;
    }
}



#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ApplicationCell";

    ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

#if USE_INDIVIDUAL_SUBVIEWS_CELL
        [[NSBundle mainBundle] loadNibNamed:@"IndividualSubviewsBasedApplicationCell" owner:self options:nil];
        cell = tmpCell;
        self.tmpCell = nil;

#endif
    }

    // Display dark and light background in alternate rows -- see tableView:willDisplayCell:forRowAtIndexPath:.
    cell.useDarkBackground = (indexPath.row % 2 == 0);

    // Configure the data for the cell.

    NSDictionary *dataItem = [data objectAtIndex:indexPath.row];
    cell.icon = [UIImage imageNamed:[dataItem objectForKey:@"Icon"]];
    cell.publisher = [dataItem objectForKey:@"Publisher"];
    cell.name = [dataItem objectForKey:@"Name"];


    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
    detailViewController. = [data objectAtIndex:indexPath.row];
   [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
}



@end

Thanks for your reply, i really appreciate it.

A: 

The table cell needs to be subclassed from UITableViewCell. You don't have to set the title of the view controller three or more times, it is always overwritten. You should set up your custom cell in the cellForRowAtIndex method and set the properties of the cell.

burki

related questions