ios - 为什么我的 JSON 数组不显示在 UITableView 对象中?

标签 ios objective-c json uitableview

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.domain.com/venues.php"]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSError *error = nil;
    NSArray *responseArray = [NSJSONSerialization JSONObjectWithData:response options:0 error:&error];
    NSLog(@"%@", responseArray); // This logs fine
}

@synthesize responseArray;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 1; // temporary replacement for return [responseArray count] for testing purposes

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier =@"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text=[responseArray objectAtIndex:indexPath.row];

    return cell;
}

为了比较,下面的代码确实有效:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.colors= [[NSArray alloc] initWithObjects: @"Red", @"Yellow", @"Green", @"Blue", @"Purple", nil];
    NSLog(@"%@", self.colors); // logs fine
}

@synthesize colors;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.colors count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier =@"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text=[self.colors objectAtIndex:indexPath.row];

    return cell;
}

更新

我的 cellForRowAtIndexPath 现在看起来像这样,但我仍然没有得到任何结果。我的 JSON 有问题吗?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier =@"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text=[[self.responseArray objectAtIndex:indexPath.row]objectForKey:@"name"];

    return cell;
}

JSON:

[{"name":"venue 1"},{"name":"venue 2"},{"name":"venue 3"}]

最佳答案

    @interface YourTableViewController ()<BViewControllerDelegate>

    @property (strong, nonatomic) NSArray *responseArray;

    @end

    @implementation yourTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
      self.responseArray = [NSJSONSerialization JSONObjectWithData:response options:0 error:&error];

}

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {
            // Return the number of rows in the section.
            return [self.responseArray count];

        }

        -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            static NSString *CellIdentifier =@"Cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

            cell.textLabel.text=[[self.responseArray objectAtIndex:indexPath.row]objectForKey:@"yourKey"];

            return cell;
        }

关于ios - 为什么我的 JSON 数组不显示在 UITableView 对象中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21332324/

相关文章:

ios - UITableView 滚动到最接近今天日期的部分

javascript - typeahead.js、localStorage 和大型 json 文件

javascript - ES6 - 遍历 JSON

python - 在 Django 中获取反序列化对象的主键

ios - 绘制时 GLKView 为空白

ios - 是否有 (self.frame.origin.y + self.frame.size.height) 的快捷方式?

当应用程序收到通知时,IOS 向 HTTP 服务器发送请求

ios - 如何初始化此Swift枚举-甚至应该使用它?

ios - 如何在 Swift 中以编程方式调整图像的 x 轴和 y 轴?

ios - 如何使用 mailcore 框架或任何框架在 swift/objective c 中从电子邮件代理获取收件箱?