如何将值从 tableview 传递到 collectionview

Posted

技术标签:

【中文标题】如何将值从 tableview 传递到 collectionview【英文标题】:How to pass the values from tableview to collectionview 【发布时间】:2014-09-11 07:08:06 【问题描述】:

我正在使用 SwReveal 视图控制器将值从表视图传递到集合视图控制器。当我单击集合视图中的单元格时,我存储了集合视图单元格的索引路径。但是当我选择一个表并将一个值传递给索引路径集合视图时。不显示值的单元格。它是空的......这是我的代码

-(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];
    cells.subjectLabel.text=@"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;
        cells.subjectLabel.text=@"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];

           

【问题讨论】:

您尝试过什么调试?这里的代码很多,光看也不好诊断。 你能试着更好地解释这个问题吗?这里的用例是什么?为什么要尝试在特定 indexPath 处获取 Cell 对象,而不是从数据源获取 if?您需要单元格,还是只需要数据?还是您要更新单元格?而什么Viewindex?从tableView中选择的indexPath? 【参考方案1】:

在我的脑海中我会说你的单元格不存在,你需要分配/初始化它或dequeueWithReuseIdentifier

另外,您正在使用“Cell”来创建您的单元格,而不是 UICollectionViewCell ;除非您使用名为 Cell 的自定义单元格,否则这可能是个问题

如果这没有帮助,您能否在描述中添加更多代码? cellforrow 和数据传递方法。

【讨论】:

Cell 是一个自定义 Cell 类 我对您的代码并不完全有信心。即使它可能无法解决任何问题,我建议您将代码位移动到不同的类(也称为 json 解析)。另外,我认为单元格是在视图出现后创建的,您是否尝试将您的内容从viewWillAppear 移动到viewDidLoad? (或类似“加载后”的地方)

以上是关于如何将值从 tableview 传递到 collectionview的主要内容,如果未能解决你的问题,请参考以下文章

将值从 VC1 传递到 VC2

将值从表视图控制器传递到视图控制器

如何将值从 javascript 函数传递到 django 视图

如何将值从片段/活动传递到 xml?

如何将值从基本适配器传递到 Android 中的 Activity

如何将值从一个活动传递到上一个活动