2009-06-14 70 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