2015-12-12 4 views
0

У меня есть локальный JSON и есть отображение на первой странице.Как выбрать строку из таблицы в iOS?

После этого я хочу выбрать одну из строк из таблицы. Когда я выбираю строку, она будет отображать другой JSON из url, разные строки имеют разные json. Тем не менее, я не знаю, как узнать строку. Я пробую много способов, но он не может работать.

Вот код первой страницы:

@implementation HKTableViewController{ 
    NSArray *_location_array,*_hki; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    JSONLoader *location_jsonLoader = [[JSONLoader alloc] init]; 
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"locations" withExtension:@"json"]; 

    //NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/funhiking/index.php/index/getHKIsland"]; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     _location_array = [location_jsonLoader locationsFromJSONFile:url]; 

     [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; 
    }); 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    DistrictTableViewController *district = segue.destinationViewController; 

    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 

    district.location = [_location_array objectAtIndex:indexPath.row]; 

    if (indexPath.row == 0) { 
     // Create a new JSONLoader with a local file URL 
     JSONLoader *jsonLoader = [[JSONLoader alloc] init]; 

     //go to web to take json file 
     NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/funhiking/index.php/index/getHKIsland"]; 

     // Load the data on a background queue... 
     // As we are using a local file it's not really necessary, but if we were connecting to an online URL then we'd need it 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
      _hki = [jsonLoader hkiFromJSONFile:url]; 
      // Now that we have the data, reload the table data on the main UI thread 
      [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; 
       }); 
    } 
} 

#pragma mark - Table View Controller Methods 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HKCell"]; 

    Locations *location = [_location_array objectAtIndex:indexPath.row]; 

    cell.textLabel.text = location.title; 
    NSLog(@"%@", location.title); 

    return cell; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [_location_array count]; 
} 
+0

использование didselectrowatindex метод делегата табличного представления –

ответ

0

Вы можете использовать didselectrowatindex метод делегата зрения таблицы после установки делегата табличного, то вы можете получить объект из массива в этом методе и и можете просто покажите URL JSON. Happy кодирование ...

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