如何将分段表视图中的数据传递给详细视图?

时间:2011-03-29 17:13:01

标签: iphone uitableview plist

我最近下载了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 个答案:

答案 0 :(得分: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"];