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

时间:2013-11-28 18:40:55

标签: ios iphone uitableview

我想制作可折叠/可扩展的tableview,其中我有两个标题产品和服务,每个包含10个对象,自定义单元格,左侧包含复选标记和标签。我检查教程,但大部分都在使用.xib而我的项目基于故事板。 我查看这些教程,任何人都可以帮我解决这个问题。

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

2 个答案:

答案 0 :(得分:3)

我在此处找到了一个很棒的示例项目:

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

这很容易理解和实施。

答案 1 :(得分:0)

//
//  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