【问题标题】:ios tableviews and json arraysios tableviews 和 json 数组
【发布时间】:2012-03-20 20:31:43
【问题描述】:

这让我困惑了好久

我成功地从一个 json 调用填充了一个数组,但是我想要填充的表视图没有像 .

谁能明白为什么?

我已经尝试了一些很好的方法,但是传递给 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

这对吗?

【问题讨论】:

  • 你在操作数组后[tableView reloadData]吗?
  • 谢谢你,但我现在在下面的编辑中遇到了问题

标签: ios json uitableview nsarray


【解决方案1】:

两件事:

在设计器中,您的 TableView 是否通过 IBOutlet 连接?此参考是必要的,以便您的代码可以向其发送说明。

其次,我没有看到你在任何地方打电话给[tableview reloadData],你需要在完成数据收集之后再打电话。

【讨论】:

  • 感谢您,我已将 tableview 的插座连接到委托和数据源。我没有重新加载数据,但现在做。我现在得到 tableView 的本地声明隐藏实例变量,我有 IBOutlet UITableView *tableView;在我的 .h
  • 只需给您的 IBOutlet 取另一个名字。
  • 再次感谢 - 你可以告诉我这对我来说是新的,我在 .m 和 .h 中给了 IBOutlet 另一个名称,但它是一样的。也许您可以快速查看一下我刚刚发布的上面的 .h 并告诉我它看起来是否正常?
  • 您是如何重命名 IBOutlet 的?简单地在编辑器中重命名是危险的,因为您可能会错过一些参考。右键单击并选择 Refactor -> Rename 是首选方式。我现在要做的是删除您的 IBOutlet(在代码和 IB 中),然后重新创建它。
  • 顺便说一句,我猜出了问题是您只在 .m 和 .h 文件中重命名了 IBOutlet。您还必须更新 IB 中的连接,而您可能没有这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2012-10-28
相关资源
最近更新 更多