Collection View Cell 行为异常
Collection View Cell behaving abnormally
我有一个单元格,其中我在顶部有两个标签,然后在中间有一个视图,在底部有两个标签,它是一个图表。我已经硬编码了项目的数量,因为我只需要 10-13 个值,显然一切都很好,值正在更新,子视图被添加到 cellForRowAt 委托方法中,但是在滚动时标签的值显示为空白,并且这种行为是非常不正常的,因为在滚动时如果第二个和第三个索引变为空白然后在进一步滚动时它们会返回并且第 7 个 oe 第 5 个变为空白,就像那样因为没有服务调用所以我不调用重新加载数据。这绝对让我发疯,我知道这与单元格何时被重用有关,但我不知道为什么或我做错了什么,因为最初这个值看起来很好。
- (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)];
}
cellForItem
当您的单元格出现在屏幕上时,即使没有重新加载也会被调用。假设您在索引 0 处的单元格的值为 A。然后您滚动并且 0 处的单元格离开屏幕。索引 8 处的单元格显示值为 B。当您返回查看索引 0 处的单元格时,tableview 可能会重复使用索引 8 处的单元格。现在,如果您没有将其设置回正确的位置,索引 0 处的单元格将显示 B索引 0 处数据的值。跟随?
当您在 cellForItem
中有如下语句时,通常会发生这种情况:
if foo {
cell.textLabel.text = "isFoo"
}
然后你不为 else 条件编写代码。
所以请确保您这样做:
if foo {
cell.textLabel.text = "isFoo"
} else {
cell.textLabel.text = "isNotFoo"
}
更新:
现在我看到了您的代码,这正是您所做的。您在 if 语句中设置了隐藏的标签,并且从不在 else 语句中取消隐藏它们。
在您的 initCellWithMaximumWeight 方法中包含以下代码。
[self.completedWeightLabel setHidden:NO];
[self.completedRepititonsLabel setHidden:NO];
[self.dateLabel setHidden:NO];
[self.setLabel setHidden:NO];
我有一个单元格,其中我在顶部有两个标签,然后在中间有一个视图,在底部有两个标签,它是一个图表。我已经硬编码了项目的数量,因为我只需要 10-13 个值,显然一切都很好,值正在更新,子视图被添加到 cellForRowAt 委托方法中,但是在滚动时标签的值显示为空白,并且这种行为是非常不正常的,因为在滚动时如果第二个和第三个索引变为空白然后在进一步滚动时它们会返回并且第 7 个 oe 第 5 个变为空白,就像那样因为没有服务调用所以我不调用重新加载数据。这绝对让我发疯,我知道这与单元格何时被重用有关,但我不知道为什么或我做错了什么,因为最初这个值看起来很好。
- (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)];
}
cellForItem
当您的单元格出现在屏幕上时,即使没有重新加载也会被调用。假设您在索引 0 处的单元格的值为 A。然后您滚动并且 0 处的单元格离开屏幕。索引 8 处的单元格显示值为 B。当您返回查看索引 0 处的单元格时,tableview 可能会重复使用索引 8 处的单元格。现在,如果您没有将其设置回正确的位置,索引 0 处的单元格将显示 B索引 0 处数据的值。跟随?
当您在 cellForItem
中有如下语句时,通常会发生这种情况:
if foo {
cell.textLabel.text = "isFoo"
}
然后你不为 else 条件编写代码。
所以请确保您这样做:
if foo {
cell.textLabel.text = "isFoo"
} else {
cell.textLabel.text = "isNotFoo"
}
更新:
现在我看到了您的代码,这正是您所做的。您在 if 语句中设置了隐藏的标签,并且从不在 else 语句中取消隐藏它们。
在您的 initCellWithMaximumWeight 方法中包含以下代码。
[self.completedWeightLabel setHidden:NO];
[self.completedRepititonsLabel setHidden:NO];
[self.dateLabel setHidden:NO];
[self.setLabel setHidden:NO];