UITableView的折叠与展开

接口文件

#import <UIKit/UIKit.h>


@interface TableFoldVC : UIViewController


@end


实现文件

#import "TableFoldVC.h"


static NSInteger const tagButton = 1000;


@interface TableFoldVC () <UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, strong) UITableView *mainTableView;

@property (nonatomic, strong) NSMutableArray *mainArray;


@property (nonatomic, strong) NSMutableArray *foldArray; // 控制列表的展开与折叠


@end


@implementation TableFoldVC


@synthesize mainTableView;

@synthesize mainArray;

@synthesize foldArray;


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.title = @"展开与折叠";

    

    [self setUI];

    

    [self setlocalData];

    

    [self.mainTableView reloadData];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void)loadView

{

    [super loadView];

    self.view.backgroundColor = [UIColor whiteColor];

}


- (void)setUI

{

    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])

    {

        [self setEdgesForExtendedLayout:UIRectEdgeNone];

    }

    

    self.mainTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    [self.view addSubview:self.mainTableView];

    self.mainTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

    self.mainTableView.backgroundColor = [UIColor colorWithRed:0.2 green:0.5 blue:0.7 alpha:0.5];

    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    self.mainTableView.delegate = self;

    self.mainTableView.dataSource = self;

    self.mainTableView.tableFooterView = [[UIView alloc] init];

}


- (void)setlocalData

{

    NSInteger count = arc4random() % 10 + 1;

    

    if (!self.mainArray)

    {

        self.mainArray = [NSMutableArray array];

    }

    

    for (NSInteger i = 0; i < count; i++)

    {

        NSString *image = @"Girl.png";

        NSString *title = [NSString stringWithFormat:@"%ld", i + 1];

        

        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:image, @"image", title, @"title", nil];

        [self.mainArray addObject:dict];

    }

    

    // 展开或折叠数据信息

    if (!self.foldArray)

    {

        self.foldArray = [NSMutableArray array];

    }

    

    for (NSInteger i = 0; i < count; i++)

    {

        NSNumber *number = [NSNumber numberWithBool:1];

        [self.foldArray addObject:number];

    }

}


- (void)foldClick:(UIButton *)button

{

    NSInteger index = button.tag - tagButton;

    

    // 重置信息

    NSNumber *number = [self.foldArray objectAtIndex:index];

    BOOL newBool = !number.boolValue;

    NSNumber *newNumber = [NSNumber numberWithBool:newBool];

    [self.foldArray replaceObjectAtIndex:index withObject:newNumber];

    

    // 刷新,展开或折叠

    NSIndexSet *sectionSet = [NSIndexSet indexSetWithIndex:index];

    [self.mainTableView reloadSections:sectionSet withRowAnimation:UITableViewRowAnimationNone];

}


#pragma mark - 列表视图


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return self.mainArray.count;

}



// 方法1 返回段标题,使用2时不使用1

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

//{

//    NSString *sectionString = [NSString stringWithFormat:@"section %d", section];

//    

//    return sectionString;

//}


// 方法2 自定义段视图

static CGFloat const sizeSpace = 10.0;

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(sizeSpace, 0.0, tableView.frame.size.width - sizeSpace * 2, 20.0);

    button.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.5];

    

    NSString *sectionString = [NSString stringWithFormat:@"section %ld", section];

    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    [button setTitle:sectionString forState:UIControlStateNormal];

  

    button.tag = section + tagButton;

    [button addTarget:self action:@selector(foldClick:) forControlEvents:UIControlEventTouchUpInside];

    

    return button;

}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 20.0;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    // 根据信息展开或折叠

    NSNumber *number = [self.foldArray objectAtIndex:section];

    if (1 == number.intValue)

    {

        return self.mainArray.count;

    }

    else if (0 == number.intValue)

    {

        return 0;

    }

    

    return 0;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *const reuseCell = @"reuseCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCell];

    if (!cell)

    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseCell];

        

        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        

        // cell.selectionStyle = UITableViewCellSelectionStyleGray;

        UIView *selectView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];

        selectView.backgroundColor = [UIColor greenColor];

        cell.selectedBackgroundView = selectView;

        

        cell.textLabel.font = [UIFont systemFontOfSize:16.0];

        cell.textLabel.textColor = [UIColor orangeColor];

        cell.textLabel.textAlignment = NSTextAlignmentRight;

        

        cell.detailTextLabel.font = [UIFont systemFontOfSize:10.0];

        cell.detailTextLabel.textColor = [UIColor lightGrayColor];

        cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;

    }

    

    NSDictionary *cellDict = [self.mainArray objectAtIndex:indexPath.row];

    

    NSString *imageString = [cellDict objectForKey:@"image"];

    UIImage *image = [UIImage imageNamed:imageString];

    cell.imageView.image = image;

    

    NSString *titleString = [cellDict objectForKey:@"title"];

    cell.textLabel.text = titleString;

    

    NSString *detailString = [NSString stringWithFormat:@"section %d, row %@", indexPath.section, titleString];

    cell.detailTextLabel.text = detailString;

    

    return cell;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 60.0;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}


@end


转载于:https://my.oschina.net/potato512/blog/647865

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值