Kavya Saravagi Kavya Saravagi - 5 months ago 38
iOS Question

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

I have a json array as-

(
(
(
SALES,
MARKETING,
LEGAL,
COMPLIANCE
),
(
Excel,
Outlook,
CMM,
Proprietary
),
(
Name,
Address,
"Phone Number",
"Email Address"
),
(
"Due Diligence",
RISK
)
),
(
(
ADVISORY,
"TRUST SERVICES",
FINANCIAL,
PLANNING,
"RETIREMENT SVCS",
"ESTATE PLANNING",
REBALANCING
),
(
Proprietary,
Morningstar,
Finantix,
"Finance Logix",
eMoney
),
(
Income,
"Net Worth",
"Liquid Net Worth",
"Time Horizon",
Purpose
),
(
"Due Diligence",
RISK,
KYC
)
)
),

)


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

Answer

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