I have a cell in which i have two labels on the top, then a view in the middle and two labels on bottom, its a graph. I have hard-coded the number of items as i only need 10-13 values, apparently everything is fine and the values are being updated and the subviews are added in the cellForRowAt delegate method but upon scrolling the values of the labels appear blank, and this behaviour is quite abnormal because on scroll if 2nd and 3rd index goes blank then on further scrolling they come back and 7th oe 5th goes blank, like that since there is no service call so i am not calling reload data. this is absolutely making me crazy i know it has to do with when the cell is being reused, but i don't know why or what is it that i am doing wrong, because initially the value appear just fine.
- (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;
}
Below are the methods called in the delegate method. `
-(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];
}
This is the second method given below
-(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)];
}
cellForItem
will get called even without a reload when you have cells appearing on to the screen. So say your cell at index 0 had a value of A. Then you scroll and cell at 0 goes off the screen. Cell at index 8 appears with value B. When you scroll back to view cell at index 0, the tableview might reuse the cell at index 8. Now the cell at index 0 would show B if you're not setting it back to the proper value for the data at index 0. Follow?
This often happens when you have a statement in cellForItem
like:
if foo {
cell.textLabel.text = "isFoo"
}
And then you don't code for the else condition.
So make sure you're doing this:
if foo {
cell.textLabel.text = "isFoo"
} else {
cell.textLabel.text = "isNotFoo"
}
UPDATE:
Now that I see your code, that's exactly what you did. You set the labels hidden in your if statement and never unhid them in your else statement.