UITableView的折叠与展开

2016/03/26 15:40
阅读数 28

接口文件

#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


展开阅读全文
打赏
1
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
1
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部