2009-06-14 106 views
0

我使用一個函數來填充字典一個陣列 在這裏是在上面的代碼陣列flashcardsNamesList,flashCardsId調用函數[自getFlashCard當改變每次代碼如何從iPhone中的nsdictionary檢索值?

-(void)getAllFlashCardsNames 
{ 
if ([listofitems count]==0) 
    listofitems = [[NSMutableArray alloc] init]; 
else 
    [listofitems removeAllObjects]; 

for(int i=0;i<listOfCategoryId.count;i++) 
{ 
    int j=[[listOfCategoryId objectAtIndex:i]intValue]; 
    [self getFlashCard:j];  
    NSArray *flashCardsNames = flashCardsNamesList; 
    NSArray *flashCardsids = flashCardsId; 
    NSLog(@"FLash Card Ids %@",flashCardsids);  
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:flashCardsNames,@"flashCards",flashCardsids,@"flashCardId",nil]; 
    [listofitems addObject:dictionary]; 

} 

}

:J- ]。 j是改變來自listOfCategoryId數組的categoryid的參數。

現在如何從字典中檢索值我想在uitableview的不同部分顯示不同的flashcardsNames。

這裏是代碼即時通訊使用檢索值

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return [listofitems count]; 

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section { 
    NSDictionary *dictionary =[listofitems objectAtIndex:section]; 
    NSLog(@"dictionary=%@",dictionary); 
    NSArray *array =[dictionary objectForKey:@"flashCards"]; 
    NSLog(@"array=%@",array); 
    NSLog(@"Section Count = %d",array.count); 
    return array.count; 
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    CustomCell *cell = (CustomCell *)[tableViewdequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) {  
     cell = [[[CustomCell alloc] initWithFrame:CGRectZero  reuseIdentifier:CellIdentifier] autorelease];  
    } 

    NSDictionary *dictionary =[listofitems objectAtIndex:indexPath.section]; 
    NSArray *array =[dictionary objectForKey:@"flashCards"]; 
    NSArray *array1=[dictionary objectForKey:@"flashCardId"]; 
    NSString *cellValue=[array objectAtIndex:indexPath.row]; 
    NSString *cellValue1=[array1 objectAtIndex:indexPath.row]; 
    [cell.FlashCardsNames setText:cellValue]; 
    [cell setFlashCardId:[cellValue1 intValue]]; 
    return cell; 
} 

但該方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath不會被調用

回答

3

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath不調用的方法

您是否將您的方法實現的對象設置爲表視圖的數據源? UITableView將一些工作交給另一個對象,該對象必須符合UITableViewDataSourceUITableViewDelegate協議;您必須將對象設置爲表視圖的dataSourcedelegate,可以用IB或編程方式(數據源和委託可以是不同的對象,但通常是同一個對象)。看看this article,它更多地解釋它;一旦完成,您的對象必須處理tableView:cellForRowAtIndexPath:tableView:numberOfRowsInSection:方法,這些方法將通過表視圖在您的對象上調用。

此外,線:

if ([listofitems count]==0) 
    listofitems = [[NSMutableArray alloc] init]; 

沒有意義。我假設你正在檢查數組是否已被分配,如果沒有,分配它。如果數組尚未分配,則它將爲nil,因此發送count至此無效。如果之前已經分配了它,但是已經釋放,但是而不是恢復爲nil這將是一個糟糕的指針並導致您的應用程序崩潰。

如果您在UIApplicationDelegate子類中實現此方法,則更好的方法是在您的類awakeFromNib方法或applicationDidFinishLaunching:方法中進行分配。不要忘記在你的dealloc方法中釋放它。

+0

您是否將您的方法實現的對象設置爲表視圖的數據源? 這是什麼意思? – 2009-06-14 17:20:06

相關問題