tableView实现二级菜单

代码很简单,直接上代码:

#import "ViewController.h"
#import "MainCell.h"
#import "SecondCell.h"

#define ExpandCount 3

static NSString *mainCell = @"mainCell";
static NSString *secondCell = @"secondCell";

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) BOOL isExpand; //是否展开
@property (strong, nonatomic) NSIndexPath *selectedIndexPath;//展开的cell的下

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _tableView.delegate=self;
    _tableView.dataSource=self;

    [_tableView registerNib:[UINib nibWithNibName:@"MainCell" bundle:nil] forCellReuseIdentifier:mainCell];
    [_tableView registerNib:[UINib nibWithNibName:@"SecondCell" bundle:nil] forCellReuseIdentifier:secondCell];
}

//返回tableview中cell的个数
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.isExpand == YES) {
        return 1 + ExpandCount;
    }
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//设置 cell的样式
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        MainCell *cell = [tableView dequeueReusableCellWithIdentifier:mainCell forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[MainCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:mainCell];
        }
        return cell;
    }
    else{
        if (self.isExpand && self.selectedIndexPath.row < indexPath.row && indexPath.row <= self.selectedIndexPath.row + ExpandCount) {   // Expand cell
            SecondCell *cell = [tableView dequeueReusableCellWithIdentifier:secondCell forIndexPath:indexPath];

            if (!cell) {

                cell = [[SecondCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:secondCell];
            }

            return cell;
        }
        return nil;
    }
}
//返回cell的高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0f;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}
//当cell被选择(被点击)时调用的函数
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        NSLog(@"点击一级cell");
        if (!self.selectedIndexPath) {

            self.isExpand = YES;

            self.selectedIndexPath = indexPath;

            NSArray *arr = [self indexPathsForExpandRow:indexPath.row];
            [tableView beginUpdates];
            [tableView insertRowsAtIndexPaths:arr withRowAnimation:(UITableViewRowAnimationBottom)];
            [tableView endUpdates];

        }else{
            if (self.isExpand) {
                if (self.selectedIndexPath == indexPath) {
                    self.isExpand = NO;

                    NSArray *arr = [self indexPathsForExpandRow:indexPath.row];
                    [tableView beginUpdates];
                    [tableView deleteRowsAtIndexPaths:arr withRowAnimation:(UITableViewRowAnimationBottom)];
                    [tableView endUpdates];

                    self.selectedIndexPath = nil;
                }
            }

        }
    }else{
        NSLog(@"点击二级cell");
    }

}

- (NSArray *)indexPathsForExpandRow:(NSInteger)row {
    NSMutableArray *indexPaths = [NSMutableArray array];
    for (int i = 1; i <= ExpandCount; i++) {
        NSIndexPath *idxPth = [NSIndexPath indexPathForRow:row + i inSection:0];
        [indexPaths addObject:idxPth];
    }
    return [indexPaths copy];
}

@end

注:MainCell SecondCell 分别是一级cell和二级cell。

转载请注明出处,万分感谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值