2016-06-23 7 views
0

私はカスタム 'UICollectionViewCell'の内部にtableViewを作成しています。私のViewControllerの中には、各行にTableViewで表示されるデータを含む配列があります。 したがって、この配列のデータをカスタムセル(tableview delegatesメソッドを含む)に渡したいと思います。ViewController内の配列データをUICollectionViewカスタムセルに渡す方法は?

私はこのようにしていますが、それは助けにはなりません。

インサイド "GroupCollectionViewCell.m"

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *groupTableIdentifier = @"DeptGroupTable"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:groupTableIdentifier]; 

    if (cell == nil) { 

    } 
    RoadmapViewController *vc = [[RoadmapViewController alloc] init]; 
    cell.textLabel.text =[vc.grouplist objectAtIndex:indexPath.row]; 
    return cell; 
} 

そして内部 "RoadViewController.m"

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    GroupCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 

    if (cell == nil) { 
     cell = [[GroupCollectionViewCell alloc] init]; 
     // [cell.groupData addObjectsFromArray:_grouplist]; 
     //:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    return cell; 
} 

- ここで、groupListもし私が渡す配列とgroupDataはカスタムセル内の空の配列です。

答えて

1

それはこのよう

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

{ 
    GroupCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 
    if (cell == nil) { 
     cell = [[GroupCollectionViewCell alloc] init]; 
     // [cell.groupData addObjectsFromArray:_grouplist]; 
     //:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    [cell loadTableData:arr]; 
    return cell; 
} 

ようcellForItemAtIndexPathからこの関数を呼び出した後、この

@property (strong, nonatomic) NSMutableArray *arrObj; 

-(void)loadTableData(NSMutableArray*)arr { 
    self.arrObj = arr; 
    [self.tableView reloadData]; 
    //Now used this arrObj in your delegate and datasource method 
} 

のようなあなたのCollectionViewCellと1メソッド内で1つのMutableArrayオブジェクトを作成しますが、これはあなたを助けることを願っています。

+0

すべては大丈夫ですが、私はしないでください。

cell.textLabel.text =[_groupData objectAtIndex:indexPath.row]; 

そして "RoadViewController.m" は、このメソッドを追加で

なぜ私は "cell"から "loadTableData"メソッドを取得できないのか知っています。 –

+0

申し訳ありません。このメソッドを 'CollectionViewCell.h'ファイルで宣言する必要があるのを忘れてしまいました。その方法を宣言すれば、それが利用可能になります。 –

+0

私はすでに宣言しています。私はcell.loadTableData(arr)は動作しないと思う。私は他の方法で使用する必要があります。 –

0

あなたがRoadmapViewControllerを作成し、そこからgrouplistを取得するためにalloc initを使用する場合、それはあなたのvc.grouplistnilを返すことは明らかです。

は、次の2行削除:

RoadmapViewController *vc = [[RoadmapViewController alloc] init]; 
cell.textLabel.text =[vc.grouplist objectAtIndex:indexPath.row]; 

をそして、次の操作を行います。

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(GroupCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{   
    cell.groupData = _groupList; 
    [collectionView reloadData]; 
} 
関連する問題