2017-07-18 58 views
0

我有一个单元格,其中顶部有两个标签,中间是一个视图,底部是两个标签,它是一个图形。我硬编码的项目数量,因为我只需要10-13值,显然一切都很好,值正在更新和子视图添加在cellForRowAt委托方法,但在滚动标签的值显示为空白,和这种行为是非常不正常的,因为如果第二个和第三个索引变为空白,那么在进一步滚动时他们会回来,第五个第五个变为空白,因为没有服务调用,所以我不调用重新加载数据。这是绝对让我疯狂的,我知道它与单元格被重用时的关系,但我不知道为什么或者是什么,我做错了,因为最初的值看起来很好。集合视图单元格行为异常

- (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;  
    }  

以下是在委托方法中调用的方法。 `

-(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]; 
} 

这低于

-(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

它很难找出没有代码的问题。 –

+0

我已经添加了代码,请看看它。 –

+0

在initEmptyCell中,你隐藏了一些东西。但在initCellWithMaximumWeight中,您没有取消隐藏它。单元格被重用(回收),因此您需要为cellForRow中的所有情况设置它们。 – GeneCode

回答

1

cellForItem将被调用,即使没有当你的细胞出现在屏幕重载给出的第二种方法。所以说你的索引0的单元格的值为A.然后你滚动,0的单元格离开屏幕。索引8处的单元格显示为值B.当您向后滚动查看索引0处的单元格时,表格视图可能会重用索引8处的单元格。现在,如果您没有将其设置回正确的位置,索引0处的单元格将显示B值为索引0处的数据。

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

然后你的其他条件不代码:

当你有一个声明cellForItem像这种情况经常发生。

所以一定要确保你这样做:

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

UPDATE:

现在,我看到你的代码,这就是你做了什么。您将标签隐藏在您的if语句中,并且永远不会在您的else语句中取消它们。

+0

谢谢。你是对的,这是我相信的问题。我已经取消了他们,现在一切正常。 –

1

将以下代码包含在您的initCellWithMaximumWeight方法中。

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

感谢@Prakash的回应。非常感激。 –

相关问题