tableView实现二级菜单

来源:互联网 发布:淘宝店铺怎么加入淘客 编辑:程序博客网 时间:2024/04/30 02:31

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

#import "ViewController.h"#import "MainCell.h"#import "SecondCell.h"#define ExpandCount 3static 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。

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

1 0
原创粉丝点击