• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何使用objective-c用两个NSMutableArray填充可扩展的TableView

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:41:00 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 Expandable UITableview由 Tom Fewster 创建。我想使用两个 NSMutableArray 来调整示例,这是一个场景,如果有人想要从 webservice json 数据填充可扩展/折叠 TreeView 表,则需要实现。因此,由于在他的示例中 GroupCell 没有数组,我想知道我该怎么做?请记住,我的 Objective-C 仍然生锈,所以我要问这个问题。

我的尝试只显示组的第一个 ObjectAtIndex:indexPath:0。

我希望能够填充表格并获得这样的输出;

  • A组
    • 第 1a 行
    • 第 2a 行
    • 第 3a 行
  • B 组
    • 第 1b 行
    • 第 2b 行
  • C组
    • 第 1c 行
    • 第 2c 行
    • 第 3c 行 等等。

如果您以这种方式更好地理解它,您也可以使用 JSON 数据来解释您的答案。

在这里我想用 JSON 数据填充表格,以便 GroupCell 显示 class_name 和 rowCell 显示 subject_name。这是我从 JSON Web 服务解析的控制台;

(
    {
    "class_id" = 70;
    "class_name" = Kano;
    subject =         (

            "subject_id" = 159;
            "subject_name" = "Kano Class";
        }
    );
},
    {
    "alarm_cnt" = 0;

    "class_id" = 71;
    "class_name" = Lagos;
    subject =         (

            "subject_id" = 160;
            "subject_name" = "Lagos Class";
        }
    );
},
    {
    "alarm_cnt" = 3;
    "class_id" = 73;
    "class_name" = Nasarawa;
    subject =         (

            "subject_id" = 208;
            "subject_name" = "DOMA Class";
        },

            "subject_id" = 207;
            "subject_name" = "EGGON Class";
        },

            "subject_id" = 206;
            "subject_name" = "KARU Class";
        },

            "subject_id" = 209;
            "subject_name" = "LAFIA Class";
        },

            "subject_id" = 161;
            "subject_name" = "Nasarawa State Class";
        }
    );
},
    {
    "alarm_cnt" = 2;
    "class_id" = 72;
    "class_name" = Rivers;
    subject =         (

            "subject_id" = 162;
            "subject_name" = "Rivers Class";
        }
    );
}

)

我试过了,这是我的片段

- (UITableViewCell *)tableViewExpandableTableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath

{ 静态 NSString *CellIdentifier = @"RowCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NSDictionary *d=[_sitesJson objectAtIndex:0] ;
NSArray *arr=[d valueForKey"subject_name"];
NSDictionary *subitems = [arr objectAtIndex:0];
NSLog(@"Subitems: %@", subitems);
NSString *siteName = [NSString stringWithFormat"%@",subitems];
cell.textLabel.text =siteName;
//}
NSLog(@"Row Cell: %@", cell.textLabel.text);
// just change the cells background color to indicate group separation
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
cell.backgroundView.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:243.0/255.0 blue:1.0 alpha:1.0];

return cell;

}

- (UITableViewCell *)tableViewExpandableTableView *)tableView cellForGroupInSectionNSUInteger)section

{ 静态 NSString *CellIdentifier = @"GroupCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *textLabel = (UILabel *)[cell viewWithTag:2];
NSDictionary *d2 = [_regionsJson objectAtIndex:0];
NSArray *arr2 = [d2 objectForKey"class_name"];
NSString *regions = [[arr2 objectAtIndex:section]objectAtIndex:0];
textLabel.textColor  = [UIColor whiteColor];
textLabel.text = [NSString stringWithFormat: @"%@ (%d)", regions, (int)[self tableView:tableView numberOfRowsInSection:section]];
NSLog(@"Group cell label: %@", textLabel.text);

// We add a custom accessory view to indicate expanded and colapsed sections
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed"ExpandableAccessoryView"] highlightedImage:[UIImage imageNamed"ExpandableAccessoryView"]];
UIView *accessoryView = cell.accessoryView;
if ([[tableView indexesForExpandedSections] containsIndex:section]) {
    accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
} else {
    accessoryView.transform = CGAffineTransformMakeRotation(0);
}
return cell;

}



Best Answer-推荐答案


他,只需要一点点更新一个方法

- (UITableViewCell *)tableViewExpandableTableView *)tableView cellForGroupInSectionNSUInteger)section
{
    static NSString *CellIdentifier = @"GroupCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSIndexPath *indexPath;
    NSString *regions = [[_dataGroup objectAtIndex:section]objectAtIndex:0];
    cell.textLabel.text = [NSString stringWithFormat: @"%@ ", regions];

    // We add a custom accessory view to indicate expanded and colapsed sections
    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed"ExpandableAccessoryView"] highlightedImage:[UIImage imageNamed"ExpandableAccessoryView"]];
    UIView *accessoryView = cell.accessoryView;
    if ([[tableView indexesForExpandedSections] containsIndex:section]) {
        accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
    } else {
        accessoryView.transform = CGAffineTransformMakeRotation(0);
    }
    return cell; 
}

可以帮到你。

HTH,享受编码!

关于ios - 如何使用objective-c用两个NSMutableArray填充可扩展的TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29667542/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap