2017-05-26 85 views
0

我是iOS開發新手,我在計算包含字典的數組中的對象時遇到了麻煩。numberOfRowsInSection和prepareForSegue方法問題

我的數據結構是這樣的,它的位於其他自定義類:

Library header file Library implementation file

會有相當多的人(現在只有4只是用於測試),我需要算與關鍵kEviela NSStrings。

我需要這個計數來計算我的tableView所需的行數:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

return ?; 

另外我有一個的DetailView,我需要單獨顯示每個該字符串。那麼我怎麼能在prepareForSegue方法中做到這一點?

回答

0

這只是

return _library.count; 

這是陣列中詞典的數量。

無論如何,我建議映射字典中的自定義類。

要獲得所有值的鍵kEviela陣列寫

NSArray *kEvielaArray = [_library valueForKey: kEviela]; 

這是其中valueForKey旨在作爲KVC方法的罕見的情況下一個。

+0

右鍵字典中的主陣列的元素個數一個NSArray是位於另一個自定義類,所以我想這樣得到這個:NSArray * library = [self.eVielasLibrary.library valueForKey:kEviela];返回[library count];但它表明圖書館是零。 –

+0

你沒有提到*其他自定義類*。編輯你的問題,並添加所有相關信息如何與類相關。 – vadian

0

首先,我想你可以通過計算你的字典對象顯示的行數。我從字典中使用NSMutableArray轉換,因爲它更容易操作。但它完全是個人的。

另外我有一個的DetailView,我需要單獨顯示每個該字符串。

你能做什麼?

有一個函數叫做didSelectRowAtIndexPath委託方法。

如果選擇行此功能called.In此功能你抓你的DetailView控制器,並根據您的數據保存價值的它是一個變量(的NSString或東西)。 您可以在分配值後從這裏以編程方式調用segue。

舉個例子:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 

    DetailViewController *detailVC = [[DetailView alloc] initWithNibName:@"DetailView" bundle:nil]; 

    detailVC.userName = [userList objectAtIndex:indexPath.row]; 

    [self.navigationController pushViewController:detailVC animated:YES]; 

} 

或者,如果你不想使用這個樣子,然後在prepareForSegue方法(你叫什麼) 抓住你的目標視圖控制器,並分配值一樣以前。而已。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Make sure your segue name in storyboard is the same as this line 
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"]) 
    { 
     // Get reference to the destination view controller 
     YourViewController *vc = [segue destinationViewController]; 
     vc.dictionary = [youdictionary mutableCopy]; 


     // Pass any objects to the view controller here, like... 
     [vc setMyObjectHere:object]; 
    } 
} 
0
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return _library.count; 

} 

上面是你數組的計數檢查它的出路。

這裏首先你有數組,然後你可以找出字典值。

NSString *kEvielaValue = [_library valueForKey: "kEviela"]; 
0

試試這個[[_library valueForKey: kEviela]count];

[_library valueForKey: kEviela]將選取包含kEviela所有值和計數的消息會給你從_library

相關問題