我有一个包含键和值的 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 函数内部)。
请帮我解决这个问题。谢谢并感激!