JLAccordion 0.2.1

JLAccordion 0.2.1

测试已测试
语言 Obj-CObjective C
许可协议 MIT
发布最后发布2016年2月

Joey Lee维护。



  • 作者:
  • Joey L.

基于UITableView的折叠控制器。

使用方法

请查看仓库中的示例项目。

结构

有两种类型的单元,父单元和子单元。您需要为每个单元创建父数据和学生数据。

设置数据

  • 创建父数据
[JLAccordionData parentDataWithIdentifier:@"parent_001"
                                 userData:@{@"title":@"Hello"}];
  • 创建学生数据
[JLAccordionData childDataWithIdentifier:@"child_001"
                        parentIdentifier:@"parent_001"
                                userData:@{@"title":@"World"}];
  • 设置父数据和学生数据
self.accordionDataController = [JLAccordionDataController alloc] init];
self.accordionDataController.accordionDelegate = self;
[self.accordionDataController setParentDataArray:(array of parent data)
                                  childDataArray:(array of child data)];

实现以下3个协议。

UITableViewDataSource UITableViewDelegate JLAccordionDelegate

#pragma mark - <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.accordionDataController numberOfRows];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JLAccordionData *data = [self.accordionDataController dataForIndex:indexPath.row];

    if( [data isParentData] ) {
        ParentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ParentCell" forIndexPath:indexPath];
        cell.titleLabel.text = data.userData[@"title"];
        cell.arrowImageView.hidden = ![self.accordionDataController hasChildForIdentifier:data.identifier];
        [cell setOpened:[self.accordionDataController isOpenedForIdentifier:data.identifier] animated:NO];

        return cell;
    }
    else {
        ChildTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChildCell" forIndexPath:indexPath];
        cell.titleLabel.text = data.userData[@"title"];
        return cell;

    }
    return nil;
}

#pragma mark - <UITableViewDelegate>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.accordionDataController toggleCellForIndexPath:indexPath tableView:tableView];

}
#pragma mark - <JLAccordionDelegate>

- (void)accordionTableView:(UITableView *)tableView shouldOpenCellForIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if([[cell class] isSubclassOfClass:[ParentTableViewCell class]]) {
        ParentTableViewCell *parentCell = (ParentTableViewCell *)cell;
        [parentCell setOpened:YES animated:YES];
    }
}
- (void)accordionTableView:(UITableView *)tableView shouldCloseCellForIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if([[cell class] isSubclassOfClass:[ParentTableViewCell class]]) {
        ParentTableViewCell *parentCell = (ParentTableViewCell *)cell;
        [parentCell setOpened:NO animated:YES];
    }
}

样本数据和演示

sample of parent data : (
    "identifier : 100, parentIdentifier : (null), userData : {title : Hello}",
    "identifier : 200, parentIdentifier : (null), userData : {title : World}",
    "identifier : 300, parentIdentifier : (null), userData : {title : Amazing}",
    "identifier : 400, parentIdentifier : (null), userData : {title : Don't worry}",
    "identifier : 500, parentIdentifier : (null), userData : {title : Be Happy}"
)
sample of child data : (
    "identifier : 101, parentIdentifier : 100, userData : {title : Hello1}",
    "identifier : 102, parentIdentifier : 100, userData : {title : Hello2}",
    "identifier : 103, parentIdentifier : 100, userData : {title : Hello3}",
    "identifier : 104, parentIdentifier : 100, userData : {title : Hello4}",
    "identifier : 201, parentIdentifier : 200, userData : {title : World1}",
    "identifier : 202, parentIdentifier : 200, userData : {title : World2}",
    "identifier : 203, parentIdentifier : 200, userData : {title : World3}",
    "identifier : 204, parentIdentifier : 200, userData : {title : World4}",
    "identifier : 501, parentIdentifier : 500, userData : {title : show me the money}",
    "identifier : 502, parentIdentifier : 500, userData : {title : operation cwal}",
    "identifier : 503, parentIdentifier : 500, userData : {title : power overwhelming}"
)

许可协议

MIT许可下发布。您可以在您的商业和非商业项目中使用代码。