Kavya Saravagi
Kavya Saravagi

Reputation: 65

How to pass array data inside ViewController to UICollectionView Custom Cell?

I am making a tableView inside custom 'UICollectionViewCell' and inside my ViewController I have an array containing data to be displayed in TableView at each row. Therefore, I want to pass the data of this array to custom cell (containing tableview delegates methods).

I am doing like this but its not helping.

Inside "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;
}

And inside "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;
}

Note - groupList if the array which I want to pass and groupData is a blank array inside custom cell.

Upvotes: 0

Views: 1155

Answers (2)

Nirav D
Nirav D

Reputation: 72460

Create one MutableArray object inside your CollectionViewCell and one method like this

@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
}

After that call this function from cellForItemAtIndexPath like this way

- (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;
}

Hope this will help you.

Upvotes: 1

The Mach System
The Mach System

Reputation: 6983

If you use alloc init to create RoadmapViewController and get grouplist from that, it is obviously that your vc.grouplist will return nil.

Remove these two lines:

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

And do this:

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

And in "RoadViewController.m" add this method:

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

Upvotes: 0

Related Questions