J'essaye de remplir un UITableView
avec les données du résultat de json. Je peux l'obtenir pour charger de plist
tableau aucun problème et je peux même voir mon json. Le problème que j'ai est que le UITableView
ne peut jamais voir les résultats de JSON. S'il vous plaît, gardez-moi car c'est ma première fois avec Objective-C.peupler UITableView à partir de json
Dans mon .h
@interface TableViewViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
NSArray *exercises;
NSMutableData *responseData;
}
Dans mon fichier .m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return exercises.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//create a cell
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
// fill it with contnets
cell.textLabel.text = [exercises objectAtIndex:indexPath.row];
// return it
return cell;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://url_to_json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self ];
// load from plist
//NSString *myfile = [[NSBundle mainBundle] pathForResource:@"exercise" ofType:@"plist"];
//exercises = [[NSArray alloc] initWithContentsOfFile:myfile];
[super viewDidLoad];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection failed: %@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *dictionary = [responseString JSONValue];
NSArray *response = [dictionary objectForKey:@"response"];
exercises = [[NSArray alloc] initWithArray:response];
}
Je reçois une erreur disant que tableView n'est pas supprimé? – mcgrailm
OK. Votre contrôleur de vues est-il un UITableViewController ou simplement un UIViewController? Vous aurez besoin d'un point de vente pour votre UITableView. Si votre contrôleur de vue est UITableViewController, remplacez le code ci-dessus par [[self tableView] reloadData]; Sinon, déclarez un objet IBoutlet UITableView dans votre fichier d'en-tête et nommez-le tableView et connectez-le dans Interface Builder. Ensuite, le code devrait fonctionner. –
ok obtenu cette partie de travail merci pour votre aide – mcgrailm