2014-01-24 2 views
0

Вот мой код:Почему мой массив 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"}] 
+1

в массиве JSon вам следует нравится это значение ключа cell.textLabel.text = [responseArray objectAtIndex: indexPath.row] valueForKey @ "ключ"]; И, на ваш взгляд, загрузили и добавили [table reloadData]; после nslog –

+0

показать свой журнал ответов? Я думаю, вы что-то пропустили. – Mani

+0

Ничего не ускользает из viewDidLoad, поскольку все назначено локальной переменной. –

ответ

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 [email protected]"Cell"; 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

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

      return cell; 
     } 
+1

Спасибо за ваш ответ. Не могли бы вы взглянуть на мое редактирование? – Sebastian

+0

это работает принято мой ответ дать репутацию – codercat

+0

звук хороший .. – codercat

Смежные вопросы