iOS tableView 下拉列表的设计

参考了SKSTableView的设计思想,工程在github上,我由于没有连接就不贴了git

想要作成下边的效果

有两种思路

思路1是用tableView的head做为一级菜单,其余都是每个section的全部cell,这种方法看起来很好数据是数据,标题是标题github

思路二是用TableView的每个section的row 0 作标题,其他的是数据,这种方法更为简单点,可是逻辑处理将会复杂一些spa

思路一通过一番实验后发现,问题主要表如今如何让headView响应点击事件,因而将tableview的headview添加一个button,而后让点击时候设置当前的section的行数为0;进而达到所须要的效果设计

重点介绍一下思路二,并贴上一些代码,由于思路二能够在没有二级菜单的时候能够方便的响应cell点击事件

设置了一个显示隐藏的状态变量_open和记录最后一次点击section的变量_lastOpen

BOOL _open;
NSInteger _lastOpen;
代理

_datasouth = [[NSMutableArray alloc] initWithArray:@[@[@"miao", @"miao1",@"miao12"],
                   @[@"nong", @"nong1", @"nong12", @"Rnong13", @"nong14", @"nong15", @"nong16", @"Rnong17"],
                   @[@"fei", @"fei1",@"fei1"],
                   @[@"lei"],
                   @[@"wu"]]];
事件

设置代理

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _datasouth.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_open == 0) {
        return 1;
    }
    if (_lastOpen == section) {
        NSArray * arr = _datasouth[section];
        return arr.count;
    }
    return 1;
}
it

对cell自定义

 if (indexPath.row == 0) {io

//组名cell
table

        if ([_datasouth[indexPath.section] count] == 1) {
            //无分组的cell自定义
ast

            //等同于底层cell,执行跳转?

        }
    } else {
        //底层cell,执行跳转?
    }

选中的方法的逻辑

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 && [_datasouth[indexPath.section] count] == 1) {
        //跳转;
        return;
    }
    if (_open == 0) {
        if (indexPath.row == 0) {
            _open = 1;
            _lastOpen = indexPath.section;
        }
    } else {
        if (indexPath.row == 0) {
            if (_lastOpen == indexPath.section) {
                _open = 0;
                _lastOpen = -1;
            }
            _lastOpen = indexPath.section;
        }
    }
    [_tableView reloadData];
    //[_tableView reloadSections:[NSIndexSet indexSetWithIndex:_lastOpen] withRowAnimation:UITableViewRowAnimationAutomatic];
    if (indexPath.row != 0) {
       //跳转
    }
}

其余的头尾视图设置(可不设置)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    if (indexPath.row == 0) {        return 40;    };    return 70;}- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {    return .01;}- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 5)];    return view;}

相关文章
相关标签/搜索