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。

轉載請註明出處,萬分感謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章