Question

Voici la dernière mise à jour.Il plante lors de la définition de textes dans indexpath.section ==0 et ==1 et ==2 avec l'erreur

[Objet d'accueilAtIndex :] :Sélecteur non reconnu envoyé à l'instance 0x109624f20 2014-07-30 12: 03: 24.732 Tym-App [1704: 60b * Arrêt de l'application en raison d'une exception non interceptée 'NSInvalidArgumentException', raison :'-[ObjetAtIndex:] :sélecteur non reconnu envoyé à l'instance 0x109624f20'

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
NSLog(@"wsolna hon");
static NSString *cellIdentifier = @"HomeTableViewCell";
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

// Configure the cell...
int row= [indexPath row];


                if (indexPath.section == 0){
                     homeObject = [homeArray[0] objectAtIndex:indexPath.row];

                         NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[homeObject.News dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
                    cell.NewsDateLabel.text= homeObject.News_Date;
                    cell.NewsLabel.attributedText= attrStr;
                    NSLog(@"news: %@", attrStr);

                }


                if (indexPath.section == 1){
                    homeObject = [homeArray[1] objectAtIndex:indexPath.row];

                  NSLog(@"value of indexpath for library: %d",row);
                         NSLog(@"library: %@",homeObject.News);
                    cell.NewsLabel.text= homeObject.Library_text;
                    cell.TestimonialNameLabel.text= @"";

            }

                if (indexPath.section == 2){
                    homeObject = [homeArray[2] objectAtIndex:indexPath.row];

                         NSLog(@"news: %@",homeObject.Testimonial_Description);
                    cell.NewsLabel.text= homeObject.Library_text;
                    cell.TestimonialNameLabel.text = homeObject.Testimonial_Name;

            }


return cell;
}
Était-ce utile?

La solution

Si la quantité de sections est fixe, je vous recommande de stocker vos données dans un tableau multidimensionnel.Ainsi, cela vous protégera de la plupart des erreurs logiques ultérieures.

// Declaring array for 3 sections
homeArray = [[NSMutableArray alloc] initWithObjects:[[NSMutableArray alloc] init], [[NSMutableArray alloc] init], [[NSMutableArray alloc] init], nil];

Effectuez ensuite la séparation logique nécessaire lors de la récupération des données et de leur placement dans le tableau.

MISE À JOUR: Comme @Wain l'a dit, il n'est pas recommandé de faire des requêtes à l'intérieur cellForRowAtIndexPath:, il sera donc préférable de placer le prochain extrait à l'endroit où vous mettez vos informations en cache

if ([moduleID isEqualToString:@"77"]){
    [homeArray[0] addObject:[[Home alloc]initWithItemID: modID andNewsName:nText andNewsDate: (NSString *) nDate andLibraryText: dText andTestDescription: tText andTestimonialName: (NSString *) tName]];
} else if ([moduleID isEqualToString:@"81"]){
    [homeArray[1] addObject:[[Home alloc]initWithItemID: modID andNewsName:nText andNewsDate: (NSString *) nDate andLibraryText: dText andTestDescription: tText andTestimonialName: (NSString *) tName]];
} else if ([moduleID isEqualToString:@"78"]){
    [homeArray[2] addObject:[[Home alloc]initWithItemID: modID andNewsName:nText andNewsDate: (NSString *) nDate andLibraryText: dText andTestDescription: tText andTestimonialName: (NSString *) tName]];
}

Et enfin, obtenez le homeObject nécessaire pour votre table de cette façon

if (indexPath.section == 0) {
    homeObject = [homeArray[0] objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
    homeObject = [homeArray[1] objectAtIndex:indexPath.row];
} else if (indexPath.section == 2) {
    homeObject = [homeArray[2] objectAtIndex:indexPath.row];
}

Autres conseils

Ceci est un problème de réutilisation cellulaire.

Pour corriger, assurez-vous que chaque fois que vous retournez une cellule de cellfrotorDindexPath: vous définissez toujours le contenu de l'étiquette de texte pour toutes les étiquettes.Cela signifie explicitement certains avec du texte et la définition des autres pour ne pas avoir de texte.

La solution alternative (et nettoyante) consiste à utiliser différentes cellules personnalisées pour chaque section.

Aussi, ne pas interroger la base de données chaque fois que vous êtes demandé une cellule.Requête une fois et cache les résultats.Il est très inutile d'interroger chaque fois et rendra votre application plus lente.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top