Collection视图中的奇怪行为

时间:2014-10-08 13:39:23

标签: ios objective-c xcode uicollectionview

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

        }

    }

//等等 }

2 个答案:

答案 0 :(得分:1)

我认为你想要的是第一项,而不是第一项。

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

        }

    }

修改

这是我处理集合视图单元格的方式。

UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];

//cell.backgroundColor = [UIColor greenColor];
UILabel *label = (UILabel *)[cell viewWithTag:101];

UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];


UIStoryboard *storyboard=[[UIStoryboard alloc] init];



currentIndexPath=indexPath;

int selectedCellNum=(int)indexPath.item;

if (selectedCellNum==0) {

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {


        storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    }
    else
    {
        storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];

    }



    recipeImageView.image = [UIImage imageNamed:@"1B copy.png"];

    label.textColor = [UIColor blackColor];

    POISTableViewController * destViewController = [storyboard instantiateViewControllerWithIdentifier:@"POISList"];



    destViewController.selectedCategory=0;

    destViewController.sightsToShow=self.groups;

    [self.navigationController pushViewController:destViewController animated:YES];







}
else if (selectedCellNum==1)
{

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {


        storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    }
    else
    {
        storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];

    }



    recipeImageView.image = [UIImage imageNamed:@"2B copy.png"];

    label.textColor = [UIColor blackColor];

    MonumentsViewController * destViewController = [storyboard instantiateViewControllerWithIdentifier:@"MonumentsViewController"];

    destViewController.sightsToShow=self.groups;

    [self.navigationController pushViewController:destViewController animated:YES];




}

答案 1 :(得分: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];