第一次钻取后 TableView 未填充

发布于 2024-11-08 14:05:49 字数 7377 浏览 1 评论 0原文

我有一个来自 pList分段表格视图,我想深入了解子视图。我唯一的问题是我坚持在第一次深入分析后填充它(主菜是唯一包含内容的)、“描述”和“标题”。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软的没边 2024-11-15 14:05:49

您可以以递归方式钻取数据。
对于根视图控制器单元格选择方法
你必须修改你的代码,

- (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
        }
    }
}

希望这能解决你的钻孔问题..:)

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.. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文