I have an UTableView grouped. I want to split my data into “categories” group. I have an instance variable which contains all groups in my database.
@interface TableViewController : UITableViewController {
// Response from server
NSString* serverResponse;
NSMutableArray *categories; // This NSMutableArray contains all category's server
}
@property (strong) NSString *serverResponse;
@property (strong) NSMutableArray *categories;
I fill my NSMutableArray with the requestFinished method (where all works perfectly)
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSString *result = request.responseString;
NSArray *jsonArray = (NSArray*)[result JSONValue];
int count = [jsonArray count];
int i;
NSDictionary *jsonDict = [[NSDictionary alloc] init];
for (i = 0; i < count; i++) {
jsonDict = [jsonArray objectAtIndex:i];
NSString *current = [jsonDict objectForKey:@"categoriy"];
[categories addObject:current];
NSLog(@"%d", [categories count]) // Shows 4 -> ok !
}
}
But when I call it in this method :
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"%d", [categories count]) // Show 0 -> bad !
return [categories count];
}
It shows on the consol “0” !
I really dont understand why !
Perhaps someone could help me with this?
The problem might be that
numberOfSectionsInTableView:is called before therequestFinished:is called.I think you have to reload the tableview.
Try putting
[self.tableView reloadData], or something like that, at the end ofrequestFinished:.