2012-03-20 65 views
0

這已經莫名其妙我詭計IOS tableviews和JSON陣列

我成功地從填充一個JSON調用數組,但表視圖我想poulate是不會發生的。

任何人都可以看到爲什麼?

我已經嘗試了好幾個事情,但數組獲得通過到的tableView是沒有得到填充成功檢索的JSON值

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    self.responseData = nil; 

    NSArray* categories = [(NSDictionary*)[responseString JSONValue] objectForKey:@"categories"]; 
    [responseString release]; 


    //fetch the data 
    for (NSDictionary* item in categories) {   
     NSString* c = [item objectForKey:@"CATEGORY_NAME"]; 
     [self.tableViewArray addObject:[NSString stringWithFormat:@"%@", c]]; 
    } 

} 

    - (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self loadData]; 
} 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [tableViewArray count]; 
} 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    // Configure the cell. 
    NSInteger rowNumber = indexPath.row; 
    NSString *stateName = [tableViewArray objectAtIndex:rowNumber]; 
    cell.textLabel.text = stateName; 
    return cell; 
} 

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *message = [NSString stringWithFormat:@"You selected %@",[self.tableViewArray objectAtIndex:indexPath.row]]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message: message delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil]; 

    [alert show]; 

    [alert release]; 


} 

編輯

我的.h是

@interface Medical_MeetingsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 
    NSMutableArray *tableViewArray; 
    NSMutableData *responseData; 
    IBOutlet UITableView *tableViewCat; 
} 

@property (nonatomic, retain) NSMutableArray *tableViewArray; 
@property (retain, nonatomic) NSMutableData *responseData; 

-(IBAction)loadData; 

@end 

這是正確的嗎?

+0

操作數組後,你是否[tableView reloadData]? – 2012-03-20 20:38:23

+0

感謝這,但我現在正在編輯下面的問題 – Herb 2012-03-20 21:26:51

回答

1

兩件事情:

在設計,你已經通過一個IBOutlet連接你的TableView?此引用是必需的,以便您的代碼可以向其發送說明。

其次,我沒有看到您在任何地方撥打[tableview reloadData],您需要在收集完數據後再調用它。

+0

感謝您的這一點,我有tableview的插座連接到委託和數據源。我沒有reloaddata,但現在做。我現在正在獲取tableView隱藏實例變量的本地聲明,我有IBOutlet UITableView * tableView;在我的.h – Herb 2012-03-20 21:23:05

+0

只需給你的IBOutlet別名。 – 2012-03-20 21:28:38

+0

再次感謝 - 你可以告訴我這對我來說是新的,我給了IBOutlet在.m和.h中的另一個名字,但它也是一樣的。也許你可以快速瀏覽一下上面剛剛發佈的.h,並告訴我它看起來好嗎? – Herb 2012-03-20 21:37:38