0

Here's my code:

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

For comparison, the following code does work:

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

Update

My cellForRowAtIndexPath now looks like this, but I'm still not getting any results. Is there a problem with my 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"}]
3
  • 1
    in your json array you shoud like this value for key cell.textLabel.text=[responseArray objectAtIndex:indexPath.row]valueForKey@"key"]; And in your view did load also add [table reloadData]; after nslog Commented Jan 24, 2014 at 12:11
  • show your response log? I think, you've missed something. Commented Jan 24, 2014 at 12:14
  • Nothing escapes from viewDidLoad since everything is assigned to a local variable. Commented Jan 24, 2014 at 12:33

1 Answer 1

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;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. Would you mind taking a look at my edit?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.