views:

820

answers:

1

i am using a function to fill dictionary in a array here is the code

    -(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];

}

}

in the above code the array flashcardsNamesList,flashCardsId changes everytime when calling the function [self getFlashCard:j]; j is a parameter to change categoryid which comes from the listOfCategoryId array..

now how do i retrieve values from the dictionary i want to show different flashcardsNames on different sections in uitableview.

here is the code i m using to retrieve values

- (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;
}

but the method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath does not get called

+3  A: 

but the method -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath does not called

Have you set the object that your method is implemented in as the data source of your table view? UITableView hands some of the work off to another object, which must conform to the UITableViewDataSource and UITableViewDelegate protocols; you must then set the object as the dataSource and delegate of the table view, either in IB or programmatically (the data source and delegate can be different objects, but are commonly the same object). Take a look at this article which explains more about it; once this has been done, your object must handle the tableView:cellForRowAtIndexPath: and tableView:numberOfRowsInSection: methods, which will be called on your object by the table view.

Also, the lines:

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

do not make sense. I assume you are checking whether the array has been allocated or not, and if not, to allocate it. If the array hasn't been allocated, it will be nil, so sending count to it will have no effect anyway. If it has been allocated previously, but deallocated but not reverted back to nil it will be a bad pointer and cause your application to crash.

A better way to allocate it would be to do so in your class's awakeFromNib method, or applicationDidFinishLaunching: method, if you are implementing this in your UIApplicationDelegate subclass. Don't forget to release it in your dealloc method.

Perspx
Have you set the object that your method is implemented in as the data source of your table view?what does this means???
Rahul Vyas
Take a look at my edit
Perspx
the method tableView:numberOfRowsInSection: method is called but this line return 0 objects in the arrayNSArray *array =[dictionary objectForKey:@"flashCards"]; i am stuck here..
Rahul Vyas
When is getAllFlashCardsNames called, and in the line NSArray *flashCardsNames = flashCardsNamesList; where does `flashCsrdsNamesList` get populated with objects?
Perspx
flashCardsNamesList; this is generated from databaseand this method getAllFlashCardsNames is i am calling by passing categoryid to get flashCardsNamesList.and category id is generated from database
Rahul Vyas