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

时间:2014-01-24 12:08:54

标签: 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"}]

1 个答案:

答案 0 :(得分:1)

    @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;
        }