Andyy
Andyy

Reputation: 495

TableView not populating after first drilldown

I have a Sectioned Tableview sourced from a pList which I want to drill down into children sub views. My only problem is I'm stuck on getting it to populate after first drill down (Entree is the only one with content), "Description" and "Title".

pList

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>Rows</key>
        <array>
            <dict>
                <key>Title</key>
                <string>Entree</string>
                <key>Children</key>
                <array>
                    <dict>
                        <key>Title</key>
                        <string>Garlic Bread</string>
                        <key>Description</key>
                        <string>Cottage loaf smeared with garlic butter and herbs</string>
                        <key>Price</key>
                        <string>8.0</string>
                    </dict>
                    <dict>
                        <key>Title</key>
                        <string>Bruschetta</string>
                        <key>Description</key>
                        <string>Veggies n shit on toast</string>
                        <key>Price</key>
                        <string>9.0</string>
                    </dict>
                </array>
            </dict>
            <dict>
                <key>Title</key>
                <string>Mains</string>
            </dict>
            <dict>
                <key>Title</key>
                <string>Dessert</string>
            </dict>
            <dict>
                <key>Title</key>
                <string>Sides</string>
            </dict>
        </array>
        <key>Title</key>
        <string>Eat</string>
    </dict>
    <dict>
        <key>Title</key>
        <string>Drink</string>
        <key>Rows</key>
        <array>
            <dict>
                <key>Title</key>
                <string>Red</string>
            </dict>

        </array>
    </dict>
</array>
</plist>

MenuViewController.h

#import <UIKit/UIKit.h>


@interface MenuViewController : UITableViewController {
    NSArray *tableDataSource;   
    NSString *CurrentTitle;
    NSInteger CurrentLevel;

}
@property (nonatomic, retain) NSArray *tableDataSource;
@property (nonatomic, retain) NSString *CurrentTitle;
@property (nonatomic, readwrite) NSInteger CurrentLevel;

@end

MenuViewController.m

    #import "MenuViewController.h"

    @implementation MenuViewController
    @synthesize tableDataSource;
    @synthesize CurrentLevel, CurrentTitle;

    - (void)viewDidLoad
    {
        [super viewDidLoad];


        if(CurrentLevel == 0) {

            self.tableDataSource = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"MenuData" ofType: @"plist"]];

            self.navigationItem.title = @"Menu";
        }
        else 
            self.navigationItem.title = CurrentTitle;
    }



    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    #warning Potentially incomplete method implementation.

        return [tableDataSource count];
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    #warning Incomplete method implementation.

        return [[[tableDataSource objectAtIndex: section] 
                 objectForKey: @"Rows"] count];
    }

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

        return [[tableDataSource objectAtIndex: section] 
                objectForKey: @"Title"];

    }

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

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

        NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];

        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }

        *//The problem is right here, im just stuck on how to access this property correctly
        **cell.textLabel.text = [dictionary objectForKey:@"Title"];***
        // Configure the cell...

        return cell;
    }

    /*
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not want the specified item to be editable.
        return YES;
    }
    */





    /*
    // Override to support conditional rearranging of the table view.
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not want the item to be re-orderable.
        return YES;
    }
    */

    #pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */

    NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];

    //Get the children of the present item.
    NSArray *Children = [dictionary objectForKey:@"Children"];

    if([Children count] == 0) {
        MenuDetailViewController *mdvController = [[MenuDetailViewController alloc] initWithNibName:@"MenuDetailView" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:mdvController animated:YES];
        [mdvController release];
    }
    else {

        //Prepare to tableview.
        MenuViewController *mvController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:[NSBundle mainBundle]];

        //Increment the Current View
        mvController.CurrentLevel += 1;

        //Set the title;
        mvController.CurrentTitle = [dictionary objectForKey:@"Title"];

        //Push the new table view on the stack
        [self.navigationController pushViewController:mvController animated:YES];

        mvController.tableDataSource = Children;

        [mvController release];
    }
}

    @end

Upvotes: 0

Views: 464

Answers (1)

Midhere
Midhere

Reputation: 664

You can drill data in recursive manner. For root view controller cell selection method you have to modify your code as

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if(self.currentLevel=0)
        return [tableDataSource count];
    else
        return 1;//You require only one section in later levels
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(self.currentLevel=0)
        return [[[tableDataSource objectAtIndex: section] objectForKey: @"Rows"] count];
    else
        return [tableDataSource count];// Number of children
}

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

    if(self.currentLevel=0)
        return [[tableDataSource objectAtIndex: section] objectForKey: @"Title"];
    else
        return self.title;//Or your custom title

}

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

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSDictionary *dictionary = nil;
    if(self.currentLevel=0)
        dictionary= [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];
    else 
       dictionary = [self.tableDataSource objectAtIndex: indexPath.row];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    cell.textLabel.text = [dictionary objectForKey:@"Title"];
    // Configure the cell...

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    if(self.currentLevel=0)
        dictionary= [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];
    else 
       dictionary = [self.tableDataSource objectAtIndex: indexPath.row];

    if([dictionary objectForKey:@"Children"]!=nil){

        //Drill down only if you have child
        //Now drilling down logic
        id Object = [dictionary objectForKey:@"Children"];
        NSString * title = [dictionary objectForKey:@"Title"];

        if([object isKindOfClass:[NSArray class]) {
            NSArray * dataSource = (NSArray *)object;
            MenuViewController * viewController = [[MenuViewController alloc] initWithNibName:@"YourNibName" bundleNameOrNil:nil];
            viewController.title = title;
            viewController.tableDataSource = dataSource;
            viewController.currentLevel = self.currentLevel+1;
            [self.navigationController pushViewController:viewController animated:YES];
            [viewController release];
        } else if([object isKindOfClass:[NSDictionary class]) {
            //Only one child in the form of dictionary
            //Initialize your detail view controller and push to nav stack
            YourDetailViewController * viewController = [[YourDetailViewController alloc] initWithNibName:@"YourNibName" bundleNameOrNil:nil];
            viewController.data = (NSDictionary*)object;//Pass the view data(price etc)
            viewController.currentLevel = self.currentLevel+1;
            [self.navigationController pushViewController:viewController animated:YES];
            [viewController release];
        } else {
            //Error drilling down is not possible
        }
    }
}

Hope this will sort out your drilling problem.. :)

Upvotes: 2

Related Questions