包含 plist 信息的详细视图

发布于 2025-01-02 09:19:14 字数 2716 浏览 2 评论 0原文

我一直在关注有关 iOS 开发的教程 - 特别是深入了解 UITableViews。我已经建立了自己的自定义 plist,但我似乎无法让 DetailViewController 填充我的 plist 信息。我真的需要一些帮助,我有点不知所措!

编辑:这里有一些详细信息...

该应用程序通过 plist 填充的 RootViewController 工作,它是一个 UITableView。当 plist 中没有任何子项时,它会更改为详细视图:

AppDelegate.m

NSDictionary *tempDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
self.data = tempDict;

RootViewController.m

- (void)viewDidLoad
    {
        [super viewDidLoad];
        if(CurrentLevel == 0) { // At the 'root' of the plist

            //Initilalize our table data source
            NSArray *tempArray = [[NSArray alloc] init];
            self.tableDataSource = tempArray;

            AppDelegate *AppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];

            self.navigationItem.title = @"PedalTome";
        }
        else
            self.navigationItem.title = CurrentTitle;
    }

稍后...

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        //Get the dictionary of the selected data source.
        NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];

        //Get the children of the present item.
        NSArray *Children = [dictionary objectForKey:@"Children"];

        if([Children count] == 0) {

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

        }
        else {

            //Prepare to tableview.
            RootViewController *rvController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];

            //Increment the Current View
            rvController.CurrentLevel += 1;

            //Set the title;
            rvController.CurrentTitle = [dictionary objectForKey:@"Title"];

            //Push the new table view on the stack
            [self.navigationController pushViewController:rvController animated:YES];

            rvController.tableDataSource = Children;

        }
    }

我的 DetailViewController。 m 为空,但占位符 self.navigationController.title 除外。

如果我理解正确,我需要将信息从 RootViewController 传递到 DetailViewController - plist 的位置和实现、索引级别(就是它的名称) plist 以及该索引级别内的字符串(在键 Detail 下)。

在这一点上,任何进步都是惊人的进步。提前致谢。

I've been following a tutorial on iOS development - specifically drill-down UITableViews. I have my own custom plist established, but I can't seem to get the DetailViewController to populate with my plist information. I could really use some help here, I'm a bit over my head!

edit: Here's some details...

The app works through a plist-populated RootViewController, which is a UITableView. When there aren't any children left in the plist, it changes to a Detail view:

AppDelegate.m

NSDictionary *tempDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
self.data = tempDict;

RootViewController.m

- (void)viewDidLoad
    {
        [super viewDidLoad];
        if(CurrentLevel == 0) { // At the 'root' of the plist

            //Initilalize our table data source
            NSArray *tempArray = [[NSArray alloc] init];
            self.tableDataSource = tempArray;

            AppDelegate *AppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];

            self.navigationItem.title = @"PedalTome";
        }
        else
            self.navigationItem.title = CurrentTitle;
    }

later on...

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        //Get the dictionary of the selected data source.
        NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];

        //Get the children of the present item.
        NSArray *Children = [dictionary objectForKey:@"Children"];

        if([Children count] == 0) {

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

        }
        else {

            //Prepare to tableview.
            RootViewController *rvController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];

            //Increment the Current View
            rvController.CurrentLevel += 1;

            //Set the title;
            rvController.CurrentTitle = [dictionary objectForKey:@"Title"];

            //Push the new table view on the stack
            [self.navigationController pushViewController:rvController animated:YES];

            rvController.tableDataSource = Children;

        }
    }

My DetailViewController.m is empty, with the exception of a placeholder self.navigationController.title.

If I'm understanding correctly, I need to pass information from RootViewController to DetailViewController - the location and implementation of the plist, the index level (is that what it's called) in the plist, and the string inside that index level (under the key Detail).

At this point, any progress is amazing progress. Thanks in advance.

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

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

发布评论

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

评论(2

桃扇骨 2025-01-09 09:19:15

您可以通过在 DetailViewController 中设置合成属性,将所需的任何信息传递给 DetailViewController,然后在 if 块内将数据传递给它。

例如,在 DetailViewController.h 中,您将具有以下内容(没有 ARC):

@property (retain, nonatomic) NSDictionary *myAwesomeDictionary;

或者,启用 ARC:

@property (strong, nonatomic) NSDictionary *myAwesomeDictionary;

然后在 DetailViewController.m 中,您将具有以下内容:

@synthesize myAwesomeDictionary;

然后您可以将代码块更改为以下内容:

if([Children count] == 0) {
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];

    [dvController setMyAwesomeDictionary:dictionary];

    [self.navigationController pushViewController:dvController animated:YES];
}

这假设您在上面几行创建的名为 dictionaryNSDictionary 是您想要在您的代码中显示的数据。 DetailViewController

然后,在 DetailViewControllerviewDidLoad: 方法中,您可以使用 self.myAwesomeDictionary 访问该字典,并使用它执行您需要执行的任何操作。

免责声明

您的代码中似乎有两件事违反了 Apple 的代码风格标准:

  1. 您的 AppDelegate 存储您的模型(您的 plist)。 - Apple 表示您不应该将全局数据/逻辑挤满您的 AppDelegate。一般来说,只在该特定类中编写专门属于该类的代码。
  2. 您没有将您的 plist 解析为自定义对象。 - 这使得编码变得困难,因为您必须不断地弄清楚通用数组和字典对象代表什么,并使您的代码对其他人来说完全无法阅读。
  3. 一些实例变量名称是大写的。例如,NSArray *Children 应为 NSArray *childrenCurrentLevel 应为 currentLevel。只有类名称的第一个字母大写。

查看 http://jlawr3nc3.github.com - 特别是我的 CompanyRecords 示例代码,了解有关如何创建课程的信息和 FunWithArrays 了解如何将 plist 解析为自定义对象。 MusicLibraryiOS 然后深入研究如何获取 plist,将其解析为自定义对象,然后将其与详细视图一起显示在 UITableView 中。

You can pass whatever information you need to your DetailViewController by setting up a synthesized property in your DetailViewController, and then passing your data to it inside your if-block.

For example, in your DetailViewController.h you would have the following (without ARC):

@property (retain, nonatomic) NSDictionary *myAwesomeDictionary;

Or, with ARC enabled:

@property (strong, nonatomic) NSDictionary *myAwesomeDictionary;

Then in DetailViewController.m you would have the following:

@synthesize myAwesomeDictionary;

Then you would change your code block to the following:

if([Children count] == 0) {
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];

    [dvController setMyAwesomeDictionary:dictionary];

    [self.navigationController pushViewController:dvController animated:YES];
}

This assumes that the NSDictionary called dictionary that you created a few lines above is the data that you'd like to show in your DetailViewController.

Then in your DetailViewController's viewDidLoad: method you can access that dictionary using self.myAwesomeDictionary and do whatever you need to do with it.

Disclaimer:

Two things seem to go against Apple's code style standards in your code:

  1. Your AppDelegate stores your model (your plist). - Apple says that you shouldn't crowd your AppDelegate with global data/logic. In general, only write code that pertains specifically to a class, in that specific class.
  2. You aren't parsing your plist into custom objects. - This makes it hard to code because you constantly have to figure out what your generic Array and Dictionary objects represent, and make your code totally unreadable for other people.
  3. Some of your instance variable names are capitalized. For example, NSArray *Children should be NSArray *children and CurrentLevel should be currentLevel. Only Class names have the first letter capitalized.

Check out http://jlawr3nc3.github.com - specifically my CompanyRecords example code for information on how to make a class and FunWithArrays for how to parse a plist into custom objects. MusicLibraryiOS then delves into how to take a plist, parse it into custom objects, and then display it in a UITableView along with a detail view.

他夏了夏天 2025-01-09 09:19:15

表视图说明符 可以满足您的需要。

指定的表视图是一个具有指定内容的iOS表视图
通过 plist 文件。其目的主要是示范性的,但也是经过设计的
用于实际产品(对于制作人员名单有用)。可与
iOS 版本 4.2 及以上。

深入研究他们的代码很可能会有所启发。

Table View Specifier May do what you need.

Specified Table View is an iOS table view that has its contents specified
via a plist file. Its purpose is largely demonstrative but is also designed
to be used in a live product (useful for credits pages). Can be used with
iOS version 4.2 and above.

A dig through their code will most likely be enlightening.

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