我有一个来自 pList 的分段表视图,我想深入查看子视图。我唯一的问题是我在第一次向下钻取(Entree是唯一一个有内容),“描述”和“标题”之后不得不填充它。
的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
答案 0 :(得分:2)
您可以以递归方式钻取数据。 用于根视图控制器单元选择方法 你必须修改你的代码
- (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
}
}
}
希望这能解决您的钻井问题.. :)