Kavya Saravagi
Kavya Saravagi

Reputation: 65

How to display Json Array data inside UICollectionViewCell having UITableview rows?

I have a json array as-

  (
            (
                    (
            xyz,
            abc,
            efg,
            hij
        ),
                    (
            suv,
            xyz,
            pqr,
            lmn
        ),
                    (
            kmn,
            mno,
            "uvw",
            "xt"
        ),
                    (
            "pqr",
            lm
        )
    ),

)

Problem is that I want to display each name in a "TableView" rows in set as given in json data array above. Thats why I am using Tableview inside "UICollectionViewCell" as enter image description here

I am doing like this

Inside "GroupCollectionViewCell"-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"TableCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

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

return cell;
}

Inside "ViewController.h"-

 - (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:_grouplist];
cell.delegate=self;
return cell;
}

Upvotes: 1

Views: 275

Answers (1)

Abhishek
Abhishek

Reputation: 39

Try this idea of grouping the array into single array and pass that.

This is a sample i tried and got output like seen below.

for (int i=0; i<responceArray.count; i++) 
{
    mainARRAY=[responceArray objectAtIndex:i];
    for (int j=0; j<mainARRAY.count; j++)
    {
         arrayForEachTable=[mainARRAY objectAtIndex:j];
         [_groupData addObject:arrayForEachTable];
    }
}

Output

To print _groupData you need to make little changes in your code

In GroupCollectionViewCell.h add

@property(strong,nonatomic) NSMutableArray *cellData;

In GroupCollectionViewCell.m

add

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.cellData = [[NSMutableArray alloc] init];
    }
    return self;
}
-(void) awakeFromNib{
    self.cellData = [[NSMutableArray alloc] init];
}

Inside the GroupCollectionViewCell.m where you have your tableView DataSoure Method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"TableCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = [self.cellData objectAtIndex:indexPath.row];

    return cell;
}

Inside "ViewController.h"-

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

        cell.cellData = [_groupData objectAtIndex:indexPath.row];//here you pass the content for printing the array
        cell.delegate = self;
        return cell;
    }

Upvotes: 4

Related Questions