2011-03-29 94 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"]; 
相關問題