Ios 集合视图单元格行为异常 我有一个单元格,上面有两个标签,中间有一个视图,底部有两个标签,这是一个图。我已经硬编码了项目的数量,因为我只需要10-13个值,显然一切都很好,值正在更新,子视图添加到cellForRowAt委托方法中,但滚动时标签的值显示为空白,这种行为是非常不正常的,因为在滚动时,如果第2和第3个索引变为空白,那么在进一步滚动时,它们会返回,第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; }

Ios 集合视图单元格行为异常 我有一个单元格,上面有两个标签,中间有一个视图,底部有两个标签,这是一个图。我已经硬编码了项目的数量,因为我只需要10-13个值,显然一切都很好,值正在更新,子视图添加到cellForRowAt委托方法中,但滚动时标签的值显示为空白,这种行为是非常不正常的,因为在滚动时,如果第2和第3个索引变为空白,那么在进一步滚动时,它们会返回,第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; },ios,objective-c,uicollectionviewcell,reuseidentifier,Ios,Objective C,Uicollectionviewcell,Reuseidentifier,下面是委托方法中调用的方法 -(void) initEmptyCell { // [self setNeedsDisplay]; // back ground colour self.backgroundColor = UIColorFromRGB(COLOUR_232323); //hiding the labels [self.completedWeightLabel setHidden:YES]; [self.completedRepititonsLabel setHidden

下面是委托方法中调用的方法

-(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处的数据设置回正确的值,则索引0处的单元格将显示B。跟着

当您在
cellForItem
中有一条语句时,通常会发生这种情况,例如:

if foo {
    cell.textLabel.text = "isFoo"
}
然后你就不用为else条件编码了

因此,请确保您正在这样做:

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


现在我看到了您的代码,这正是您所做的。您可以在if语句中设置隐藏的标签,并且从不在else语句中取消隐藏标签。

cellForItem
在屏幕上显示单元格时,即使不重新加载,也会调用这些标签。假设索引0处的单元格的值为a。然后滚动,0处的单元格将退出屏幕。索引8处的单元格显示为值B。当您向后滚动以查看索引0处的单元格时,tableview可能会重用索引8处的单元格。现在,如果没有将索引0处的数据设置回正确的值,则索引0处的单元格将显示B。跟着

当您在
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];

initCellWithMaximumWeight方法中包括以下代码

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

没有代码很难发现问题。我已经添加了代码,请看一看。在initEmptyCell中,你隐藏了东西。但在initCellWithMaximumWeight中,您并没有将其取消隐藏。单元格是重复使用(回收)的,因此您需要在cellForRow中为所有情况设置它们。没有代码很难发现问题。我已经添加了代码,请查看一下。在initEmptyCell中,您可以隐藏东西。但在initCellWithMaximumWeight中,您并没有将其取消隐藏。单元格是重复使用(回收)的,所以您需要为cellForRow中的所有案例设置它们。谢谢。你说得对,我相信这就是问题所在。我已经解开了,一切都很好。谢谢。你说得对,我相信这就是问题所在。我已经解开了它们,现在一切正常。谢谢@Prakash的回复。非常感谢您的回复@Prakash。非常感谢。