2012-04-30 162 views
0

我有一個UTableView分組。我想將我的數據分成「類別」組。我有一個實例變量,其中包含我的數據庫中的所有組。無法訪問實例變量

@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; 

我填寫我的NSMutableArray與requestFinished方法(所有工作完全)

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

但是,當我把它在這個方法:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    NSLog(@"%d", [categories count]) // Show 0 -> bad ! 
    return [categories count]; 
} 

它顯示了康壽「 0「!

我真的不明白爲什麼!

也許有人可以幫助我呢?

回答

0

問題可能是在調用requestFinished:之前調用numberOfSectionsInTableView:

我認爲你必須重新加載tableview。

試着把[self.tableView reloadData],或類似的東西,在requestFinished:的末尾。

+0

完美,謝謝你們兩位! :) – Edelweiss