2011-03-29 91 views
0

我最近下载了Jeff LaMarche的分段表格视图,并试图实现一个细节视图。问题是我不知道把什么放在我的didSelectRow和detailView.m的viewDidLoad上。我为此使用了一个plist。如何将数据从分区表视图传递到详细视图?

这是我的表视图的配置: 代码:

//SectionsViewController.h 

    NSDictionary *allNames; 
    NSMutableDictionary *names; 
    NSMutableArray  *keys; 

//SectionsViewController.m 
- (void)viewDidLoad { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"]; 
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
    self.allNames = dict; [dict release]; } 

//on cellForRowAtIndexPath: 

     NSInteger section = [indexPath section]; 
    NSInteger row = [indexPath row]; 

    NSString *key = [keys objectAtIndex:section]; 
    NSArray *nameSection = [names objectForKey:key]; 

    NSDictionary *dictionary = [nameSection objectAtIndex:row]; 

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

     return cell; 

//on didSelectRowAtIndexPath 

     NSString *selectedItem =[[nameSection objectAtIndex:row] objectForKey:@"Title"]; 

     Detail *dvController = [[Detail alloc] initWithNibName:@"Detail" bundle:[NSBundle mainBundle]]; 
     [self.navigationController pushViewController:dvController animated:YES]; 

    dvController.selectedItem = selectedItem; 

    [dvController release]; 
    dvController = nil; 
    NSLog(@"teste1"); 


//Detail.m 

NSDictionary *details = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"]]; 

details = [details objectForKey:self.selectedItem]; 

titleLabel.text = [selectedItem objectForKey:@"Title"]; 
descriptionLabel.text = [selectedItem objectForKey:@"description"]; 

我的plist是这样的: 代码:

<dict> 
    <key>3 jan</key> 
    <array> 
     <dict> 
      <key>Title</key> 
      <string>asdf</string> 
      <key>description</key> 
      <string>asdqqq</string> 
     </dict> 
    </array> 
    <key>4 Jan</key> 
    <array> 
     <dict> 
      <key>Title</key> 
      <string>asddww</string> 
      <key>description</key> 
      <string>asdd</string> 
     </dict> 
    </array> 
</dict> 
</plist> 

有谁知道我在做什么错? 任何帮助表示赞赏!

谢谢!

回答

1

您需要一种将细节传递给您的Details控制器的方法。有几个选项正在使用属性或自定义初始值设定项。

//Detail.h 
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle details:(NSString*)details; 
    //And/or 
@property(copy) NSString *details; 

原始代码

//on didSelectRowAtIndexPath 
NSString *selectedItem =[[nameSection objectAtIndex:row] objectForKey:@"Title"]; 
Detail *dvController = [[Detail alloc] initWithNibName:@"Detail" bundle:[NSBundle mainBundle]]; 
dvController.details = selectedItem; 
[self.navigationController pushViewController:dvController animated:YES]; 

编辑

你可能有其他的问题,一个可能是您尝试使用@“标题”得到objectForKey:,如果你是不正确尚未钻入日期。你的PLIST是一个字典数组字典。以下是访问数组中值的示例。

NSArray *values = [dictionary objectForKey:@"3 Jan"]; 
NSDictionary *anyValue = [values lastObject]; 
NSString *title = [anyValue objectForKey:@"Title"]; 
NSString *description = [anyValue objectForKey:@"description"]; 
相关问题