ios - 充满 plist 信息的 Detail View

标签 ios uitableview uiview

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

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

该应用程序通过 plist 填充的 RootViewController 工作,这是一个 UITableView。当 plist 中没有任何 child 时,它会更改为详细 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;
    }

稍后...

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

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

最佳答案

您可以通过在您的 DetailViewController 中设置一个合成属性,然后将您的数据传递给您的 if block 内的它,将您需要的任何信息传递给您的 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)。 - Apple 说你不应该让你的 AppDelegate 充满全局数据/逻辑。通常,只编写专门属于某个类的代码,在该特定类中。
  2. 您没有将 plist 解析为自定义对象。 - 这使得编码变得困难,因为您必须不断弄清楚您的通用 Array 和 Dictionary 对象代表什么,并使您的代码对其他人来说完全不可读。
  3. 你的一些实例变量名是大写的。例如,NSArray *Children 应该是 NSArray *childrenCurrentLevel 应该是 currentLevel。只有类名的首字母大写。

查看 http://jlawr3nc3.github.com - 具体来说,我的 CompanyRecords 示例代码提供了有关如何创建类的信息,FunWithArrays 提供了有关如何将 plist 解析为自定义对象的信息。 MusicLibraryiOS 然后深入研究如何获取 plist,将其解析为自定义对象,然后将其与详细 View 一起显示在 UITableView 中。

关于ios - 充满 plist 信息的 Detail View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9122156/

相关文章:

ios - 直接从我自己的 iOS 应用程序打开 Microsoft Office 文档进行就地编辑

ios - 以 NSDictionary 而不是 NSData 的形式接收响应

ios - UITableViewCell 的内容落在单元格之外

ios - 如何使用iPhone麦克风输入进行开关以计数轮转?

ios - UIPageViewController 中包含的 ViewController 的委托(delegate)为零

ios - 如何在 ios swift 的 tableview 中为 uilabel 添加滑动手势?

ios - 读取 plist 文件。 iOS编程

ios - 没有堆叠的多个 UIView 数组

ios - 在 iOS 的 ViewController 中声明 View 时,为什么在 Swift 中使用 "weak"关键字

ios - 将触摸事件转发到另一个被接收事件的 View 部分覆盖的 View