Ryan Tsang
Ryan Tsang

Reputation: 35

How to pass data from uitableview to detail view in storyboard

I'm creating my first app in iOS 5 using storyboards. I have a tableviewcontroller embedded in a navigation controller and when you click on the cells in the tableviewcontroller some detail about that cell topic & a URL should be passed to a detail view. I use a PList to populate the tableview. I failed to pass the detail to detail view...

Here is my code (4 classes):

//  NormalDataCell.m
#import "NormalDataCell.h"


@implementation NormalDataCell


@synthesize image = _image;
@synthesize nameLabel = _nameLabel;
@synthesize category = _category;
@synthesize webSiteURL = _webSiteURL;


@end

//  normalData.m
#import "NormalData.h"


@implementation NormalData


@synthesize name = _name;
@synthesize image = _image;
@synthesize webSiteURL = _webSiteURL;
@synthesize category = _category;


- (id)initWithItem:(NSDictionary *)item {

    self = [super init];
    if (self) {
        _name = [item objectForKey:@"name"];
        _image = [UIImage imageNamed:[item objectForKey:@"image"]];
        _webSiteURL = [item objectForKey:@"webSiteURL"];
        _category = [item objectForKey:@"category"];
    }
    return self;
}


@end


@implementation NormalCategory
@synthesize category = _category;
@synthesize normalList = _normalList;


- (id)initWithArray:(NSMutableArray *)normalList {

    self = [super init];
    if (self) {
        NSMutableArray *list = [[NSMutableArray alloc]init];
        for (NSDictionary *item in normalList) {
            NormalData *data = [[NormalData alloc]initWithItem:item];
            [list addObject:data];
        }

        NormalData *data = [list objectAtIndex:0];
        _category = data.category;
        _normalList = list;
    }
    return self;
}


@end

//  normalViewController.m

#import "NormalViewController.h"

#import "NormalData.h"

#import "NormalDataCell.h"

#import "NormalDetailView.h"



@interface NormalViewController ()

{

    @private

    NSMutableArray *_cellContentList;

}



@end



@implementation NormalViewController



@synthesize tableView; // Add this line of code

@synthesize roleArray;



- (void)viewDidLoad

{

    [super viewDidLoad];



    NSURL *url = [NSURL URLWithString:@"http://vfttx.web.fc2.com/normal.plist"];

    NSMutableArray *array = [NSMutableArray arrayWithContentsOfURL:url];



    _cellContentList = [[NSMutableArray alloc]init];

        for (NSMutableArray *item in array)

    {

        NormalCategory *category = [[NormalCategory alloc]initWithArray:item];

        [_cellContentList addObject:category];

    }

   }

//How do I pass the name & webSiteURL to DetailView?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"TableToWebSegue"]) {

    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    NSString *cellName = [[_cellContentList objectAtIndex:path.row] objectForKey:@"name"];

    [segue.destinationViewController setDetailItem2:cellName];

        NSString *urlPath = [[_cellContentList objectAtIndex:path.row] objectForKey:@"webSiteURL"];

        [segue.destinationViewController setWebSiteURL:urlPath];

}
}


#pragma mark - Table view data source



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [_cellContentList count];

}



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NormalCategory *category = [_cellContentList objectAtIndex:section];

    return [category category];

}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    NormalCategory *category = [_cellContentList objectAtIndex:section];

    return [category.normalList count];

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @"normalDataCell";

    NormalDataCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];



    // Configure the cell...

    if (cell == nil) {

        cell = [[NormalDataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }



    NormalCategory *category = [_cellContentList objectAtIndex:[indexPath section]];

    NormalData *data = [category.normalList objectAtIndex:[indexPath row]];



    [cell.nameLabel setText:data.name];

    [cell.image setImage:data.image];

    cell.webSiteURL = data.webSiteURL;



    return cell;

}



@end

//  NormalDetailView.m
#import "NormalDetailView.h"


@interface NormalDetailView ()


@end


@implementation NormalDetailView


@synthesize webSiteURL;
@synthesize webView;
@synthesize name = _name;
@synthesize detailItem2;
@synthesize testNameLabel;


- (void)viewDidLoad
{
    [super viewDidLoad];

    testNameLabel.text = detailItem2;


//If the URL is right, it will work.
    NSString *strUrl = webSiteURL;
    strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}


@end

Upvotes: 3

Views: 1286

Answers (1)

Nick
Nick

Reputation: 9860

Presumably you're using segues to move between the different views.

Implement - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender on the view controller containing your table view and it will be called after the user taps the cell. It contains a reference to the segue object which you can use to find the sourceViewController and the destinationViewController, you can use these to set the data in the destination.

Upvotes: 2

Related Questions