【问题标题】:How to display Json Array data inside UICollectionViewCell having UITableview rows?如何在具有 UITableview 行的 UICollectionViewCell 中显示 Json Array 数据?
【发布时间】:2016-06-23 10:32:51
【问题描述】:

我有一个 json 数组作为-

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

)

问题是我想在上面的 json 数据数组中给出的集合中的“TableView”行中显示每个名称。这就是为什么我在“UICollectionViewCell”中使用 Tableview 作为

我就是这样做的

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

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

【问题讨论】:

  • 你的意思是在第一个 CollectionViewCell Excel、Outlook、CMM、下一个 Cell 的 tableView 中的 tableview 中说 SALES、MARKETING、LEGAL、COMPLIANCE?
  • 你的 _groupData 有合适的值吗?
  • 是的,只是这样......@AbhishekPM
  • 我的 _groupData 具有与我发布的 json 数组相同的值。 @Signare
  • _groupData 的计数是多少?

标签: ios objective-c arrays uitableview uicollectionview


【解决方案1】:

尝试将数组分组为单个数组并传递它的想法。

这是我尝试过的示例,得到的输出如下所示。

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

要打印 _groupData,您需要对代码进行少许更改

在 GroupCollectionViewCell.h 添加

@property(strong,nonatomic) NSMutableArray *cellData;

在 GroupCollectionViewCell.m 中

添加

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

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

在您拥有 tableView DataSoure 方法的 GroupCollectionViewCell.m 内

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

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

【讨论】:

  • 什么是 responseArray 和 arrayForEachTableData?
  • 您从 json 响应中处理的第一个数组是“responseArray”“mainArray”将为存储在“arrayForEachTableData”中的每个表提供一组数组
  • 你能告诉我你是如何在方法 "(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath" @AbhishekPM 中打印数组 "_groupData"
  • 我已经编辑了我的答案,它将按照您的要求打印 _groupData。
  • 嘿@AbhishekPM 你能把这个项目发给我吗,因为我正在尝试这个,在我的情况下它的打印“(”无处不在。请帮忙。拜托。
猜你喜欢
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多