2014-09-11 86 views
2

我使用SwReveal視圖控制器將值從表視圖傳遞到集合視圖控制器。當我點擊集合視圖中的單元格時,我存儲了集合視圖單元格的索引路徑。但是當我選擇一個表並將值傳遞給索引路徑集合視圖時。單元格不顯示值。它是空...這裏是我的代碼如何將值從tableview傳遞到collectionview

-(void)viewWillAppear:(BOOL)animated{ 
    text=seltext; 
    NSLog(@"text %@",text); 

// HERE I AM GETTING VALUES FROM TABLE VIEW CONTROLLER 

    self.displayindex=Viewindex; 
    NSLog(@"self dispaly %@",Viewindex); 

    // HERE I AM GETTING INDEX PATH OF SELECTED COLLECTION CELL 

    Cell *cells = (Cell *)[collectionData cellForItemAtIndexPath: self.displayindex]; 
    [email protected]"djhfkjdshfjkhd"; 
    cells.cellView.backgroundColor=[UIColor greenColor]; 
    NSLog(@"cell is %@",cells); 
    NSLog(@"text %@",text); 

// CELL IS NULL HERE AND NOTHING IS DISPLAYING EVEN IF I PASS THE CELL FRO ITEM INDEXPATH 

     } 




- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // we're going to use a custom UICollectionViewCell, which will hold subject and room number 
    Cell *myCell = [collectionData dequeueReusableCellWithReuseIdentifier:@"TableCell" 



                  forIndexPath:indexPath]; 
    if ((indexPath.row == 20) || (indexPath.row == 21) || (indexPath.row == 23) || (indexPath.row == 24)) 
    { 
     myCell.hidden=YES; 
    } 




    // if (indexPath.row < 20 || indexPath.row == 22 ) 
if (indexPath.row < 10 || (indexPath.row >14 && indexPath.row <20) ||indexPath.row ==22) 
{ 


    if (indexPath.row == 0) { 
     NSArray *Object= [[jsonData valueForKey:@"TimeTabledPeriods"]objectAtIndex:0]; 


     //myCell.roomLabel.text = [NSString stringWithFormat:@"{%ld,%ld}", (long)indexPath.section , (long)indexPath.row]; 
     myCell.roomLabel.text= [NSString stringWithFormat:@"Room: %@", [Object valueForKey:@"RoomDescription"]]; 

     myCell.subjectLabel.text = [Object valueForKey:@"SubjectDescription"]; 

     // myCell.cellView.backgroundColor= [UIColor grayColor]; 
     [myCell.redXbuttonOutlet setTag:indexPath.row]; 
     [myCell.LearningChoiceYellowOutlet setHidden:YES]; 


     [myCell.redXbuttonOutlet addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

    } 
    else (indexPath.row == 1) { 

     NSArray *Object= [[jsonData valueForKey:@"TimeTabledPeriods"]objectAtIndex:3]; 
     NSLog(@"objectATIndex 3 %@",Object); 


     //myCell.roomLabel.text = [NSString stringWithFormat:@"{%ld,%ld}", (long)indexPath.section , (long)indexPath.row]; 
     myCell.roomLabel.text= [NSString stringWithFormat:@"Room: %@", [Object valueForKey:@"RoomDescription"]]; 

     myCell.subjectLabel.text = [Object valueForKey:@"SubjectDescription"]; 
     //myCell.cellView.backgroundColor= [UIColor grayColor]; 
     [myCell.redXbuttonOutlet setTag:indexPath.row]; 
     [myCell.LearningChoiceYellowOutlet setHidden:YES]; 
     [myCell.redXbuttonOutlet addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     // ..... some more code here just like above 
    } 


-(void)buttonPressed:(id)sender 
{ 

     NSLog(@"tag number is = %d",[sender tag]); 
     NSLog(@"Button"); 


//Storing index path when cancel Button is clicked 
     deletindexpath = [NSIndexPath indexPathForRow:[sender tag] inSection:0]; 
    _appDFel.appindex=deletindexpath; 

     NSLog(@" _appDFel.appindex %@", _appDFel.appindex); 


     alert =[[UIAlertView alloc]initWithTitle:@"Delete session" message:@"Do you want to delete this session" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     alert.delegate=self; 
     [alert show]; 

    } 




- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    NSLog(@"indexpath %@",deletindexpath); 
    if (deletindexpath!=nil) { 

//enabling SWReveal view conttroller after cancel button is clicked   

     Cell *cells = (Cell *)[collectionData cellForItemAtIndexPath:deletindexpath]; 

     NSLog(@"cell %@",cells.subjectLabel.text); 
     NSString *textString=cells.subjectLabel.text; 
     _appDFel.TextString=textString; 
     [email protected]"NOSession"; 
     cells.cellView.backgroundColor=[UIColor redColor]; 
     [cells.redXbuttonOutlet removeFromSuperview]; 
     NSLog(@"swiping is done here"); 
     [self fetchinfOfData]; 
     [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer]; 
     //[self performSegueWithIdentifier:@"SwipeLeftSegue" sender:nil]; 

      } 

}

+0

你有什麼試圖對此進行調試?這裏有很多代碼,通過閱讀它並不容易診斷。 – 2014-09-15 14:55:23

+0

你可以嘗試更好地解釋這個問題嗎?這裏的用例是什麼?你爲什麼試圖在特定的indexPath中獲取Cell對象,而不是從dataSource中獲取?你需要這個單元格,還是僅僅是數據?還是你想更新一個單元? ** ** Viewindex是什麼?從'tableView'中選擇的indexPath? – Sti 2014-09-20 17:30:11

回答

4

在我的腦子裏,我會說你的電池不存在,則需要分配/初始化它或dequeueWithReuseIdentifier的頂部。

此外,您使用「單元格」來創建您的單元格,而不是UICollectionViewCell;這可能是一個問題,除非你正在使用一種名爲Cell

如果這沒有幫助的自定義單元格,您可以加入更多的代碼,你的描述? cellforrow和數據傳遞方法。

+0

單元格是自定義單元格類別 – iworld 2014-09-15 14:15:52

+1

我對您的代碼不完全有信心。即使它不能解決任何問題,我建議你將代碼移動到不同的類(又稱json解析)。另外,我認爲單元格是在視圖出現之後創建的,您是否嘗試將您的東西從'viewWillAppear'移動到'viewDidLoad'? (或類似於「加載後」的地方) – 2014-09-22 08:12:53

相關問題