2017-07-18 3 views
0

J'ai une cellule dans laquelle j'ai deux étiquettes en haut, puis une vue au milieu et deux étiquettes en bas, c'est un graphique. J'ai codé en dur le nombre d'éléments car j'ai seulement besoin de 10-13 valeurs, apparemment tout va bien et les valeurs sont mises à jour et les sous-vues sont ajoutées dans la méthode de transfert cellForRowAt mais en défilant les valeurs des étiquettes apparaissent vides, et ce comportement est tout à fait anormal car défiler si 2ème et 3ème index devient vide puis sur défilement plus tard ils reviennent et 7ème oe 5 devient vide, comme ça puisqu'il n'y a aucun appel de service donc je n'appelle pas recharger des données. Cela me rend complètement folle je sais que cela a à voir avec quand la cellule est réutilisée, mais je ne sais pas pourquoi ou qu'est-ce que je fais mal, parce qu'au début, la valeur semble très bien.Collection Vue Cellule se comportant anormalement

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
TraingingGraphCollectionViewCell *cell = (TraingingGraphCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"TraingingGraphCollectionViewCell"] forIndexPath:indexPath]; 

if (indexPath.row == 0 || indexPath.row == 12) { 

    [cell initEmptyCell]; 
} else { 

    [cell initCellWithMaximumWeight:100 completedWeight:10*(int)indexPath.row maxRepititions:10 completedRepititions:(int)indexPath.row]; 
} 

return cell;  
    }  

Voici les méthodes appelées dans la méthode déléguée. `

-(void) initEmptyCell { 
// [self setNeedsDisplay]; 

// back ground colour 
self.backgroundColor = UIColorFromRGB(COLOUR_232323); 

//hiding the labels 
[self.completedWeightLabel setHidden:YES]; 
[self.completedRepititonsLabel setHidden:YES]; 
[self.dateLabel setHidden:YES]; 
[self.setLabel setHidden:YES]; 
} 

C'est la deuxième méthode donnée ci-dessous

-(void) initCellWithMaximumWeight:(float)maxWeight 
       completedWeight:(float)completedWeight 
       maxRepititions:(int)maxRepititions 
     completedRepititions:(int)completedRepititions { 

//reloading the content 
//[self setNeedsDisplay]; 


// back ground colour 
self.backgroundColor = UIColorFromRGB(COLOUR_232323); 

// adding the weight bar 
float weightPercentage = completedWeight/maxWeight; 
if (weightPercentage > 1) { 
    weightPercentage = 1; 
} 
UIView *weightView = [[UIView alloc] initWithFrame:CGRectMake(20.0, self.graphView.frame.size.height - (self.graphView.frame.size.height * weightPercentage), 10.0, self.graphView.frame.size.height * weightPercentage)]; 
[weightView setBackgroundColor:UIColorFromRGB(COLOUR_E9280E)]; 
[self.graphView addSubview:weightView]; 

// adding the repititions bar 
float repsPercentage = (float)completedRepititions/(float)maxRepititions; 
if (repsPercentage > 1) { 
    repsPercentage = 1; 
} 
UIView *repsView = [[UIView alloc] initWithFrame:CGRectMake(35.0, self.graphView.frame.size.height - (self.graphView.frame.size.height * repsPercentage), 10.0, self.graphView.frame.size.height * repsPercentage)]; 
[repsView setBackgroundColor:UIColorFromRGB(COLOUR_97F619)]; 
[self.graphView addSubview:repsView]; 

//bottom view 
[self.dateLabel setAttributedText:[NSString stringWithFormat:@"%@", [Utilities convertDateToString:[NSDate date] usingFormat:dd__MM_DATE_FORMAT forCulture:@"nl_NL"]] withFont:FontUsingMacro(LATO_BOLD_FONT_STRING, 12.0) kerining:1.3 color:UIColorFromRGB(COLOUR_FFFFFF)]; 
[self.setLabel setAttributedText:[NSString stringWithFormat:@"Set 01"] withFont:FontUsingMacro(LATO_BOLD_FONT_STRING, 12.0) kerining:1.3 color:UIColorFromRGB(COLOUR_808080)]; 

//top view 
[self.completedWeightLabel setAttributedText:[NSString stringWithFormat:@"%.1f KG", completedWeight] withFont:FontUsingMacro(LATO_REGULAR_FONT_STRING, 12.0) kerining:1.3 color:UIColorFromRGB(COLOUR_E9280E)]; 
[self.completedRepititonsLabel setAttributedText:[NSString stringWithFormat:@"%d Reps", completedRepititions] withFont:FontUsingMacro(LATO_REGULAR_FONT_STRING, 12.0) kerining:1.3 color:UIColorFromRGB(COLOUR_97F619)]; 


} 
+0

difficile de trouver le problème sans code. –

+0

J'ai ajouté le code, jetez-y un oeil. –

+0

Dans initEmptyCell, vous masquez les choses. mais dans initCellWithMaximumWeight, vous ne l'avez pas masqué. Les cellules sont réutilisées (recyclées), vous devez donc les configurer pour tous les cas dans cellForRow. – GeneCode

Répondre

1

cellForItem sera appelé même sans recharger lorsque vous avez des cellules apparaissant à l'écran. Donc, dites que votre cellule à l'index 0 avait une valeur de A. Ensuite, vous faites défiler et la cellule à 0 disparaît de l'écran. Cellule à l'index 8 apparaît avec la valeur B. Lorsque vous revenez à afficher la cellule à l'index 0, la tableview peut réutiliser la cellule à l'index 8. Maintenant la cellule à l'index 0 affichera B si vous ne le définissez pas à la bonne valeur pour les données à l'indice 0. Suivez?

Cela arrive souvent quand vous avez une déclaration à cellForItem comme:

if foo { 
    cell.textLabel.text = "isFoo" 
} 

Et vous ne codent pas pour la condition else.

Alors assurez-vous que vous faites ceci:

if foo { 
    cell.textLabel.text = "isFoo" 
} else { 
    cell.textLabel.text = "isNotFoo" 
} 

MISE À JOUR:

Maintenant que je vois votre code, c'est exactement ce que vous avez fait. Vous définissez les étiquettes cachées dans votre instruction if et ne les annulez jamais dans votre instruction else.

+0

Merci. vous avez raison, c'était la question que je crois. Je les ai démasqués et tout fonctionne bien maintenant. –

1

Incluez le code ci-dessous dans votre méthode initCellWithMaximumWeight.

[self.completedWeightLabel setHidden:NO]; 
[self.completedRepititonsLabel setHidden:NO]; 
[self.dateLabel setHidden:NO]; 
[self.setLabel setHidden:NO]; 
+0

Merci pour la réponse @Prakash. très appréciée. –