【问题标题】:Detail View filled with plist information填充 plist 信息的详细视图
【发布时间】:2017-07-04 16:44:24
【问题描述】:

我一直在关注有关 iOS 开发的教程 - 特别是深入了解 UITableViews。我建立了自己的自定义 plist,但似乎无法让 DetailViewController 填充我的 plist 信息。我真的可以在这里使用一些帮助,我有点过头了!

编辑:这里有一些细节......

应用程序通过一个 plist 填充的 RootViewController 运行,它是一个 UITableView。当 plist 中没有任何子级时,它会更改为 Detail 视图:

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 键下)。

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

【问题讨论】:

  • 嘿克里斯蒂安,我相信有人会在接下来的 5 秒内回答这个问题,但要获得更一般的 Objc/iOS 帮助,请查看jlawr3nc3.github.com,这是我目前正在处理的示例集合。
  • 美丽的网站杰克。我已将其添加为书签以供将来参考。有什么与这个问题相关的吗?
  • 谢谢,使用 documentup.com 从我的 github README 降价文件生成。我在下面回答了你的问题。随意使用 cmets 部分提问/告诉我我完全错了:)

标签: ios uitableview uiview


【解决方案1】:

您可以通过在您的DetailViewController 中设置一个综合属性,然后在您的 if 块中将您的数据传递给它,将您需要的任何信息传递给您的 DetailViewController

例如,在您的 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)。 - 苹果说你不应该用全局数据/逻辑来拥挤你的AppDelegate。通常,只在特定类中编写与特定类相关的代码。
  2. 您没有将 plist 解析为自定义对象。 - 这使得编码变得困难,因为您必须经常弄清楚您的通用 Array 和 Dictionary 对象代表什么,并使您的代码对其他人完全不可读。
  3. 您的一些实例变量名称是大写的。例如,NSArray *Children 应该是 NSArray *childrenCurrentLevel 应该是 currentLevel。只有类名的首字母大写。

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

【讨论】:

  • 我查看了您的 FunWithArrays 应用程序,我的问题是我的应用程序的性质要求能够在 plist 中向下钻取到我在 plist 中指定的程度。这在 FunWithArrays 中可行吗?
  • 它并没有完全涵盖这一点,但 FunWithArrays 与 MusicLibraryiOS 结合使用,确实涵盖了在表格视图中向下钻取到单个对象的对象数组。我不确定这是否正是您所需要的。由于我们有点偏离问题的主题,所以我会向您发送一封包含我的联系信息的电子邮件。
【解决方案2】:

Table View Specifier 可以做你需要的。

Specified Table View 是一个 iOS 表格视图,它指定了它的内容 通过 plist 文件。它的目的主要是示范性的,但也是经过设计的 用于实时产品(适用于学分页面)。可与 iOS 4.2 及以上版本。

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

【讨论】:

    猜你喜欢
    • 2020-07-15
    • 1970-01-01
    • 2022-01-09
    • 2013-04-09
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    相关资源
    最近更新 更多