包含 plist 信息的详细视图
我一直在关注有关 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过在
DetailViewController
中设置合成属性,将所需的任何信息传递给DetailViewController
,然后在 if 块内将数据传递给它。例如,在
DetailViewController.h
中,您将具有以下内容(没有 ARC):或者,启用 ARC:
然后在
DetailViewController.m
中,您将具有以下内容:然后您可以将代码块更改为以下内容:
这假设您在上面几行创建的名为
dictionary
的NSDictionary
是您想要在您的代码中显示的数据。DetailViewController
。然后,在
DetailViewController
的viewDidLoad:
方法中,您可以使用self.myAwesomeDictionary
访问该字典,并使用它执行您需要执行的任何操作。免责声明:
您的代码中似乎有两件事违反了 Apple 的代码风格标准:
AppDelegate
存储您的模型(您的 plist)。 - Apple 表示您不应该将全局数据/逻辑挤满您的AppDelegate
。一般来说,只在该特定类中编写专门属于该类的代码。NSArray *Children
应为NSArray *children
,CurrentLevel
应为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 yourDetailViewController
, and then passing your data to it inside your if-block.For example, in your
DetailViewController.h
you would have the following (without ARC):Or, with ARC enabled:
Then in
DetailViewController.m
you would have the following:Then you would change your code block to the following:
This assumes that the
NSDictionary
calleddictionary
that you created a few lines above is the data that you'd like to show in yourDetailViewController
.Then in your
DetailViewController
'sviewDidLoad:
method you can access that dictionary usingself.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:
AppDelegate
stores your model (your plist). - Apple says that you shouldn't crowd yourAppDelegate
with global data/logic. In general, only write code that pertains specifically to a class, in that specific class.NSArray *Children
should beNSArray *children
andCurrentLevel
should becurrentLevel
. 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.表视图说明符 可以满足您的需要。
深入研究他们的代码很可能会有所启发。
Table View Specifier May do what you need.
A dig through their code will most likely be enlightening.