2013-07-07 166 views
0

我有一個包含鍵和值的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 

我有使用上面的代碼有兩個問題:

  1. 從plist中讀取它後,該列表不顯示。例如,標題「它怎麼會出錯」顯示在第一個列表中,因爲它知道它在第四個列表中。

  2. 我得到異常的說法:

    - [UINavigationController的setBookTitleValue:]:無法識別的選擇發送到實例0x894c230

這是指行:

destViewController.bookTitleKey = [[self.bookTitles allKeys]  objectAtIndex:indexPath.row]; 
destViewController.bookTitleValue = [[self.bookTitles allValues] objectAtIndex:indexPath.row]; 

(內prepareForSegue函數)。

請幫我解決這個問題。謝謝你,感謝你!

+0

[Abizem's answer below](http://stackoverflow.com/a/17513180/795339)是一種對你的第一個問題很好。對於你的第二個問題,你的segue的'destinationViewController'可能是'UINavigationController',它包含你的'BookLessoniewController'作爲它的頂級VC。請參閱[我的建議的方法](http://stackoverflow.com/a/8041666/795339)處理此問題。 –

+0

看看這個答案排序NSDictionary和轉換爲NSArray。 http://stackoverflow.com/questions/4558639/sort-an-nsmutabledictionary –

+0

嗨Esker。我仍然不明白如何解決問題2,你能否指出你的「建議方法」中的哪一部分能夠解決我的問題,而不是2?我仍然是新手,只是學習3天的ios ... =) –

回答

1

上可行的解決方案的更多細節問題2:

  1. 添加UIStoryboardSegue+MMNavigationController.h到您的項目,在this Github repo
  2. 導入發現,在您的實現頭文件在您的視圖控制器的實現文件
  3. 更改一行prepareForSegue:sender:像這樣:
BookLessonViewController *destViewController = segue.topLevelDestinationViewController; 

說明:如果故事板segue指向指向UINavigationController的目標視圖控制器,則segue的destinationViewController值將爲導航控制器對象。此處引用的代碼將通過檢查destinationViewController是否爲UINavigationController類的實例來處理此問題,如果是,將返回它的topViewController。這全部作爲UIStoryboardSegue類實施; categories是向現有類添加方法或屬性以擴展其功能而不需要繼承等的一種方法。

+0

這個工程!你能解釋那裏發生了什麼?以及在這種情況下category.h的片段是什麼? –

+0

非常感謝,你爲我節省了很多時間。 –

2

它看起來像你的plist是用來生成一本字典。 NSDictionary中的鍵(以及值)不是有序的。所以你拿到鑰匙的順序是不固定的。

所以,當你假定一個關鍵和一個值在一個特定的位置,這是一個錯誤的假設。

您應該使用數組作爲數據源而不是字典。

+0

很酷的洞察力!謝謝!你是否有任何解決方案讓對象具有鍵和值,但是當第一次插入對象時會進行排序,這將是第一個對象。謝謝。 –

+1

給它一個存儲它的訂單的鑰匙 – Abizern