Вот мой код:Почему мой массив 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 [email protected]"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 [email protected]"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text=[self.colors objectAtIndex:indexPath.row];
return cell;
}
Update
Мой cellForRowAtIndexPath
теперь выглядит, но я до сих пор не получаю любые результаты. Есть ли проблема с моим JSON?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier [email protected]"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"}]
в массиве JSon вам следует нравится это значение ключа cell.textLabel.text = [responseArray objectAtIndex: indexPath.row] valueForKey @ "ключ"]; И, на ваш взгляд, загрузили и добавили [table reloadData]; после nslog –
показать свой журнал ответов? Я думаю, вы что-то пропустили. – Mani
Ничего не ускользает из viewDidLoad, поскольку все назначено локальной переменной. –