ios故事板上可折叠/可扩展的表视图



我想制作可折叠/可扩展的表视图,其中我有两个标题产品和服务,每个标题都包含10多个带有自定义单元格的对象,该单元格左侧包含复选标记和标签。我查看了教程,但大多数都在使用.xib,而我的项目是基于故事板的。我查看了这些教程,有人能帮我吗。

https://www.cocoacontrols.com/controls/collapseclick
https://www.cocoacontrols.com/controls/ratreeview
https://www.cocoacontrols.com/controls/combobox-for-uitableview

我在上找到了一个很好的示例项目

https://github.com/singhson/Expandable-Collapsable-TableView

它很容易理解和实现。

//
//  ViewController.h
//  expandableTV
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
    NSMutableIndexSet *expandedSections;
}
@property (weak, nonatomic) IBOutlet UITableView *tbl_expandablecategory;
@end
//
//  ViewController.m
//  expandableTV
//

#import "ViewController.h"
#import "expandableTC.h"`
@interface ViewController ()
{
    NSArray *jsonArray;
    BOOL currentlyExpanded;
}
@end
@implementation ViewController
@synthesize tbl_expandablecategory;
NSMutableArray *arr_categorymenumodel;

- (void)viewDidLoad {
    [super viewDidLoad];
    arr_categorymenumodel=[[NSMutableArray alloc] init];
    if (!expandedSections)
    {
        expandedSections = [[NSMutableIndexSet alloc] init];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;//[arr_categorymenumodel count];
}
- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
    return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self tableView:tableView canCollapseSection:section])
    {
        if ([expandedSections containsIndex:section])
        {
            return 2;
        }
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        return 40;
    }
    return 270;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Configure the cell...
    if (!indexPath.row)
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        cell.textLabel.text =@"Room ";// [NSString stringWithFormat:@"%@",[[arr_categorymenumodel objectAtIndex:indexPath.section] valueForKey:@"CategoryName"]];
        //  cell.backgroundColor=[UIColor colorWithRed:237.0/255.0 green:237.0/255.0 blue:237.0/255.0 alpha:1.0];
        cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue" size:20.0];
        if (currentlyExpanded)
        {

        }
        cell.accessoryView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"rightarrow.png"]];
        return cell;
    }
    else
    {
        expandableTC *cell = [tableView dequeueReusableCellWithIdentifier:@"Customcell"];
        return cell;
    }

}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self tableView:tableView canCollapseSection:indexPath.section])
    {
        if (!indexPath.row)
        {
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            NSInteger section = indexPath.section;
            currentlyExpanded = [expandedSections containsIndex:section];
            NSInteger rows;
            NSMutableArray *tmpArray = [NSMutableArray array];
            if (currentlyExpanded)
            {
                rows = [self tableView:tableView numberOfRowsInSection:section];
                [expandedSections removeIndex:section];
                cell.accessoryView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"rightarrow.png"]];
            }
            else
            {
                [expandedSections addIndex:section];
                rows = [self tableView:tableView numberOfRowsInSection:section];
                cell.accessoryView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"downarrow.png"]];
            }
            for (int i=1; i<rows; i++)
            {
                NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
                [tmpArray addObject:tmpIndexPath];
            }
            if (currentlyExpanded)
            {
                [tableView deleteRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationFade];
            }
            else
            {
                [tableView insertRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationFade];
            }
        }
    }
}

@end

最新更新