J'ai une question. Je JSON obtenir de mon URL, il ressemble à ceci:Objectif-C JSON à TableView très lent
- (NSMutableArray *)parseObject:(NSString *)object withKey:(NSInteger)key {
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *randomKey = [standardUserDefaults stringForKey:@"randomKey"];
NSString *urlString = [NSString stringWithFormat:@"http://domain.com"];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray* latestLoans = [json objectForKey:@"object"];
NSDictionary* loan = [latestLoans objectAtIndex:key];
NSArray *myWords = [[loan objectForKey:object] componentsSeparatedByString:@","];
return myWords;
}
Pour ma TableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[self parseObject:@"bedrijfsnaam" withKey:0] objectAtIndex:indexPath.row];
//cell.detailTextLabel.text = [[self parseObject:@"leverunix" withKey:0] objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 3;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Il charge très lentement et j'ai une sorte de lag quand je veux faire défiler. Que puis-je faire pour améliorer cela?
Merci
J'ai un tableau dans viewDidLoad et met toutes mes données dans ce tableau, cela fonctionne parfaitement maintenant. Merci –
Plus généralement, vous effectuez une opération de blocage sur le thread principal, ce qui signifie que la cellule n'est pas créée tant que la réponse JSON n'a pas été reçue et analysée. Si le serveur prend 30 secondes pour répondre, votre application ne répondra pas pendant 30 secondes !!! –
@HenkdeBoer: Vous êtes les bienvenus. - Mais jetez aussi un oeil à la réponse de timthetoolman. Même une seule demande d'URL peut prendre beaucoup de temps, donc extraire les données de manière asynchrone (ou dans un thread d'arrière-plan) pour éviter que l'interface principale ne soit bloquée est également un bon conseil. –