2012-03-20 71 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