2013-03-12 35 views
2

你好,我試圖從json解析數據,並獲取值顯示在單元格中。從json數組設置cell.textlabel.text

這裏是我的ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
{ 
    IBOutlet UITableView *mainTableView; 
    NSMutableData *data; 
} 
@property (nonatomic, strong) NSArray *category; 
@property (nonatomic, strong) NSArray *coursesArray; 
@property (nonatomic, strong) NSDictionary *parsedData; 

@end 

這是我在ViewController.m

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    parsedData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
    coursesArray = [parsedData valueForKey:@"courses"]; 
    NSLog(@"coursesArray: %@", coursesArray); 
    category = [coursesArray valueForKey:@"category"]; 
    NSLog(@"%@", category); 
} 
-(int)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:@"MainCell"]; 
    } 

    cell.textLabel.text = [NSString stringWithFormat:@"%@", [category objectAtIndex:indexPath.row]]; 
    NSLog(@"%@", category); 
    return cell; 
} 

在connectionDidFinishLoading我在登錄正確的價值觀看到的,但是當我嘗試檢查他們在單元格創建之前我得​​到空。 我在這裏做錯了什麼?

回答

1

呼叫

[mainTableView reloadData]; 

-connectionDidFinishLoading:方法內。

+0

謝謝!這個伎倆。 – Pahnev 2013-03-12 08:38:52

0

確保您的類別數組未被釋放/自動釋放。