[ios]集合视图中的古怪行为

发布时间: 2017/3/28 21:21:49
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个集合视图与 25 细胞为一个单元格创建文本颜色为白色。现在当我重新加载集合视图的其他单元格的文本颜色也更改为白色颜色一个接一个当过我重新加载集合视图。我不知道问题所在。这里是我的代码。

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

        }

    }

等等}

解决方法 1:

还有什么好"奇怪"这种行为,这是不正确处理细胞重用的结果。将文本设置为白里,第一如果子句,但永远不会将其设置回任何其他的颜色在其他 if 子句。所以,当重复使用该单元格,则仍将它的文本设置为白色。它也会出现代码,不该颜色变化是同一内部的你如果每个条款,所以你根本就不应该所有这些如果每个项目; 条款你需要重构您的代码以消除所有重复。在任何如果条款,如果你真的还需要任何人,你应该设置文本颜色改为唯一项目 0,白色和黑色为他人所有,

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