ios - Collection View 中的奇怪行为

标签 ios objective-c xcode uicollectionview

我有一个包含 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/

相关文章:

ios - 为什么 iPhone 在硬件 -> 设备中不可用

ios - Cordova 构建 iOS 错误 : archive not found at path 'path/to/myApp.xcarchive"

ios - 为什么 UITableView 位于 TabBar 下方?

ios - 如何从不同UIViewController中的UITableView访问UITableViewCell?

objective-c - UIAlertView 中的其他按钮标题

ios - uiwebview 即使有 delegate = self 也没有加载请求

XCode Interface Builder 背景色

ios - 使用完成 block 并返回对象的方法

ios - UIButton 类似于 UIView 的自定义子类的行为

ios - 我如何重新填充 TableView Controller ?