2014-10-08 75 views
0

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

     } 

    } 

//等 }

+0

現在,這是的tableView或的CollectionView – EridB 2014-10-08 13:42:01

+0

colelction視圖 – iOSDeveloper 2014-10-08 13:47:36

+0

你可以添加其他單元的代碼嗎? – hoya21 2014-10-08 13:47:36

回答

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




} 
+0

沒有變化...它的行爲與之前的相同 – iOSDeveloper 2014-10-08 13:57:33

+0

嘗試更改if if else。或者它可以工作。或者使用indexPath值進行一些調試,並且如果它得到錯誤的條件代碼。 – hoya21 2014-10-08 14:01:24

+0

與我所做的一樣...我認爲它的行爲和細胞太多 – iOSDeveloper 2014-10-08 14:27:52