我有一個包含鍵和值的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中讀取它後,該列表不顯示。例如,標題「它怎麼會出錯」顯示在第一個列表中,因爲它知道它在第四個列表中。
我得到異常的說法:
- [UINavigationController的setBookTitleValue:]:無法識別的選擇發送到實例0x894c230
這是指行:
destViewController.bookTitleKey = [[self.bookTitles allKeys] objectAtIndex:indexPath.row];
destViewController.bookTitleValue = [[self.bookTitles allValues] objectAtIndex:indexPath.row];
(內prepareForSegue函數)。
請幫我解決這個問題。謝謝你,感謝你!
[Abizem's answer below](http://stackoverflow.com/a/17513180/795339)是一種對你的第一個問題很好。對於你的第二個問題,你的segue的'destinationViewController'可能是'UINavigationController',它包含你的'BookLessoniewController'作爲它的頂級VC。請參閱[我的建議的方法](http://stackoverflow.com/a/8041666/795339)處理此問題。 –
看看這個答案排序NSDictionary和轉換爲NSArray。 http://stackoverflow.com/questions/4558639/sort-an-nsmutabledictionary –
嗨Esker。我仍然不明白如何解決問題2,你能否指出你的「建議方法」中的哪一部分能夠解決我的問題,而不是2?我仍然是新手,只是學習3天的ios ... =) –