【发布时间】:2013-07-04 23:30:11
【问题描述】:
我有一个包含 key 和 value 的 plist...让我们说:
key: alpha_male_body_language
value: Alpha Male Body Language
key: building_attraction
value: Building Attraction
key: fifteen_lessons
value: Fifteen Lessons
key: how_can_it_have_gone_wrong
value: How can It Have Gone Wrong
这是我对 tableview 的实现:
#import "BookTitleViewController.h"
#import "BookLessonViewController.h"
@interface BookTitleViewController ()
@end
@implementation BookTitleViewController{
NSDictionary *bookTitles;
}
@synthesize tableView;
@synthesize bookTitles;
- (void)viewDidLoad
{
[super viewDidLoad];
self.bookTitles = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"book" ofType:@"plist"]];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
self.bookTitles = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.bookTitles count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSString *value = [[self.bookTitles allValues] objectAtIndex:indexPath.row];
//cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
cell.textLabel.text = value;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"ShowBookLesson"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
BookLessonViewController *destViewController = segue.destinationViewController;
destViewController.bookTitleKey = [[self.bookTitles allKeys] objectAtIndex:indexPath.row];
destViewController.bookTitleValue = [[self.bookTitles allValues] objectAtIndex:indexPath.row];
//destViewController.recipeName = [recipes objectAtIndex:indexPath.row];
}
}
@end
我使用上面的代码有两个问题:
从 plist 读取列表后,列表未按顺序显示。例如标题“怎么会出错”显示在第一个列表中,知道它在第 4 个列表中。
-
我得到异常说:
-[UINavigationController setBookTitleValue:]:无法识别的选择器发送到实例 0x894c230
这是指行:
destViewController.bookTitleKey = [[self.bookTitles allKeys] objectAtIndex:indexPath.row];
destViewController.bookTitleValue = [[self.bookTitles allValues] objectAtIndex:indexPath.row];
(在 prepareForSegue 函数内部)。
请帮我解决这个问题。谢谢并感激不尽!
【问题讨论】:
-
Abizem's answer below 适合您的第一个问题。对于您的第二个问题,您的 segue 的
destinationViewController可能是一个UINavigationController,它包含您的BookLessoniewController作为其顶级VC。请参阅my suggested approach 处理此问题。 -
查看这个答案以对 NSDictionary 进行排序并转换为 NSArray。 stackoverflow.com/questions/4558639/sort-an-nsmutabledictionary
-
嗨埃斯克。我仍然不知道如何解决 2 号问题。您能否指出您的“建议方法”中的哪一部分可以解决我的问题 2 号?我还是新手,刚学ios 3天... =)
标签: ios objective-c segue property-list