具有展开收缩功能的UITableview

              

该效果的主要实现方式:点击section后创建section内的cell

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

{
    NSArray *sectionTitles;
    NSArray *cellTitle1;
    NSArray *cellTitle2;
    NSArray *cellTitle3;
    NSArray *cellTitle4;
    BOOL *flag;
    
    NSInteger selectIndex;
    
    UITableView *tableView;
}

@property (nonatomic, strong) NSArray *sectionTitles;
@property (nonatomic, strong) NSArray *cellTitle1;
@property (nonatomic, strong) NSArray *cellTitle2;
@property (nonatomic, strong) NSArray *cellTitle3;
@property (nonatomic, strong) NSArray *cellTitle4;
@property (nonatomic) BOOL *flag;
@property (nonatomic, strong) UITableView *tableView;

@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize sectionTitles, cellTitle1, cellTitle2, cellTitle3, cellTitle4, flag, tableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    cellTitle1 = [[NSArray alloc] initWithObjects:@"section1", @"cell1--a", @"cell1--b", @"cell1--c", nil];
    cellTitle2 = [[NSArray alloc] initWithObjects:@"section2", @"cell2--a", @"cell2--b", @"cell2--c", nil];
    cellTitle3 = [[NSArray alloc] initWithObjects:@"section3", @"cell3--a", @"cell3--b", @"cell3--c", nil];
    cellTitle4 = [[NSArray alloc] initWithObjects:@"section4", @"cell4--a", @"cell4--b", @"cell4--c", nil];
    sectionTitles = [[NSArray alloc] initWithObjects:cellTitle1, cellTitle2, cellTitle3, cellTitle4, nil];
    flag = (BOOL *)malloc([sectionTitles count]);
    
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, [[UIScreen mainScreen] bounds].size.height) style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor colorWithRed:242.0 / 255.0 green:242.0 / 255.0 blue:242.0 / 255.0 alpha:1];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}

#pragma mark - tableview datasource && delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [sectionTitles count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40.0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (flag[section] && selectIndex == section)
    {
        return ([[sectionTitles objectAtIndex:section] count] - 1);
    }
    else
    {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.contentView.backgroundColor = [UIColor whiteColor];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
    sectionView.backgroundColor = [UIColor colorWithRed:250.0 / 255.0 green:85.0 / 255.0 blue:0 alpha:1];
    
    UILabel *sectionLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 2.5, 300, 40)];
    sectionLable.text = [[sectionTitles objectAtIndex:section] objectAtIndex:0];
    sectionLable.textColor = [UIColor whiteColor];
    [sectionView addSubview:sectionLable];
    
    
    UIButton *sectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sectionBtn.backgroundColor = [UIColor clearColor];
    sectionBtn.frame = CGRectMake(0, 0, 320, 45);
    sectionBtn.tag = section + 1000;
    [sectionBtn addTarget:self action:@selector(sectionClick:) forControlEvents:UIControlEventTouchUpInside];
    [sectionView addSubview:sectionBtn];
    return sectionView;
}

- (void)sectionClick:(UIButton *)button
{
    if ((button.tag - 1000) != selectIndex)
    {
        if (flag[selectIndex] == YES)
        {
            flag[selectIndex] = !flag[selectIndex];
        }
    }
    selectIndex = button.tag - 1000;
    flag[selectIndex] = !flag[selectIndex];
    [tableView reloadData];
    
    if (flag[selectIndex])
    {
        [tableView setContentOffset:[tableView rectForHeaderInSection:selectIndex].origin animated:YES];
        [tableView flashScrollIndicators];
    }
    selectIndex = button.tag - 1000;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    lable.backgroundColor = [UIColor colorWithRed:38.0 / 255.0 green:191.0 / 255.0 blue:205.0 / 255.0 alpha:1];
    lable.textColor = [UIColor whiteColor];
    lable.text = [NSString stringWithFormat:@"    %@", [[sectionTitles objectAtIndex:indexPath.section] objectAtIndex:indexPath.row + 1]];
    [cell.contentView addSubview:lable];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值