user1297842
user1297842

Reputation: 27

UITableView to detail UITableView drill down using storyboard

I am trying to develop a RSS Reader drill down table with storyboard in a Tab bar app. I have managed to populate my RootTableViewController with the parsed XML. I am now having a problem working out how to get each row in my RootTableViewController to point and to pass the data from the selected cell to another DetailTableViewController.

This is part of my code to parse the XML and to populate the RootTableViewController:

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"AdvCurrentCelly";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    NSString *description =   [[stories objectAtIndex: storyIndex] objectForKey: @"description"];
    NSString *title = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];

    //This populates the prototype cell 'AdvCurrentCelly'
    cell.textLabel.text = title;
    //cell.textLabel.text = date;
    cell.detailTextLabel.text = description

    return cell;

}

In Storyboard, the name of the segue from the RootTableViewContoller cell to the DetailTableViewController is ShowADVDetail

Help much appreciated

Jan

Upvotes: 0

Views: 1075

Answers (1)

audub
audub

Reputation: 435

You can pass any type of data, but I'll show you how to pass your Title string. Lets call it myString. First you need to add a property in your DetailTableViewController.h to store your string:

@property (strong, nonatomic) NSString *myString

In you RootTableViewController you need to tell the segue what to do. Use this code as an example:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Refer to the correct segue
    if ([[segue identifier] isEqualToString:@"ShowADVDetail"]) {

        // Reference to destination view controller
        DetailTableViewController *vc = [segue destinationViewController];

        // get the selected index
        NSInteger selectedIndex = [[self.teamTable indexPathForSelectedRow] row];

        // Pass the title (from your array) to myString in DetailTableViewController: 
        vc.myString = [NSString stringWithFormat:@"%@", [[stories objectAtIndex:selectedIndex] objectForKey: @"Title"]];
    }
}

Upvotes: 1

Related Questions