gpt4 book ai didi

ios - Collection View 中的奇怪行为

转载 作者:行者123 更新时间:2023-11-28 21:56:48 25 4
gpt4 key购买 nike

我有一个包含 25 个单元格的 Collection View 对于一个单元格,我将文本颜色创建为白色。现在,当我重新加载 Collection View 时,其他单元格的文本颜色也会在我重新加载 Collection View 时一个一个地变为白色。我不知道问题出在哪里。这是我的代码。

        if (indexPath.item == 0)
{
myCell.hidden=NO;
NSLog(@"index path = %ld",(long)indexPath.row );
NSArray *Object= [[jsonData valueForKey:@"TimeTabledPeriods"]objectAtIndex:0];
NSLog(@"object %@",Object);
myCell.roomLabel.text= [NSString stringWithFormat:@"Room: %@", [Object valueForKey:@"RoomDescription"]];
myCell.subjectLabel.text = [Object valueForKey:@"SubjectDescription"];
[myCell.redXbuttonOutlet setTag:indexPath.row];
[myCell.redXbuttonOutlet addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
myCell.noSessionView.backgroundColor=[UIColor clearColor];
myCell.myLearningChoicesBackground.hidden=YES;

if ([[Object valueForKey:@"Changed"] isEqualToString:@"YES"])
{
if ([[Object valueForKey:@"ChangedColorCode"] isEqualToString:@"Present"])
{
myCell.noSessionView.backgroundColor=[UIColor colorWithRed:97.0/255.0 green:182.0/255.0 blue:73/255.0 alpha:1];

//The below lines to change the text color to white is written only for index path 0
myCell.roomLabel.textColor=[UIColor whiteColor];
myCell.subjectLabel.textColor=[UIColor whiteColor];

}

}
//code for other cells

if (indexPath.item == 1)
{
myCell.hidden=NO;
NSLog(@"index path = %ld",(long)indexPath.row );
NSArray *Object= [[jsonData valueForKey:@"TimeTabledPeriods"]objectAtIndex:0];
NSLog(@"object %@",Object);
myCell.roomLabel.text= [NSString stringWithFormat:@"Room: %@", [Object valueForKey:@"RoomDescription"]];
myCell.subjectLabel.text = [Object valueForKey:@"SubjectDescription"];
[myCell.redXbuttonOutlet setTag:indexPath.row];
[myCell.redXbuttonOutlet addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
myCell.noSessionView.backgroundColor=[UIColor clearColor];
myCell.myLearningChoicesBackground.hidden=YES;

if ([[Object valueForKey:@"Changed"] isEqualToString:@"YES"])
{
if ([[Object valueForKey:@"ChangedColorCode"] isEqualToString:@"Present"])
{
myCell.noSessionView.backgroundColor=[UIColor colorWithRed:97.0/255.0 green:182.0/255.0 blue:73/255.0 alpha:1];

}

}

if (indexPath.item == 3)
{
myCell.hidden=NO;
NSLog(@"index path = %ld",(long)indexPath.row );
NSArray *Object= [[jsonData valueForKey:@"TimeTabledPeriods"]objectAtIndex:0];
NSLog(@"object %@",Object);
myCell.roomLabel.text= [NSString stringWithFormat:@"Room: %@", [Object valueForKey:@"RoomDescription"]];
myCell.subjectLabel.text = [Object valueForKey:@"SubjectDescription"];
[myCell.redXbuttonOutlet setTag:indexPath.row];
[myCell.redXbuttonOutlet addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
myCell.noSessionView.backgroundColor=[UIColor clearColor];
myCell.myLearningChoicesBackground.hidden=YES;

if ([[Object valueForKey:@"Changed"] isEqualToString:@"YES"])
{
if ([[Object valueForKey:@"ChangedColorCode"] isEqualToString:@"Present"])
{
myCell.noSessionView.backgroundColor=[UIColor colorWithRed:97.0/255.0 green:182.0/255.0 blue:73/255.0 alpha:1];

}

}

//等等

最佳答案

这种行为没有什么“奇怪”的,这是没有正确处理单元重用的结果。您在第一个 if 子句中将文本设置为白色,但绝不会在其他 if 子句中将其设置回任何其他颜色。因此,当该单元格被重复使用时,它的文本仍将设置为白色。看起来每个 if 子句中的代码,除了颜色变化之外都是相同的,所以你甚至不应该为每个项目都拥有所有这些 if 子句;您需要重构代码以消除所有重复。在任何 if 子句之外,如果您实际上仍然需要它们中的任何一个,则应仅将第 0 项的文本颜色设置为白色,将所有其他项设置为黑色,

myCell.roomLabel.textColor = (indexPath.item == 0)? [UIColor whiteColor] : [UIColor blackColor];
myCell.subjectLabel.textColor= (indexPath.item == 0)? [UIColor whiteColor] : [UIColor blackColor];

关于ios - Collection View 中的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26258380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com