参考了SKSTableView的设计思想,工程在github上,我因为没有链接就不贴了
想要做成下边的效果
有两种思路
思路1是用tableView的head作为一级菜单,其他都是每一个section的所有cell,这种方法看起来很好数据是数据,标题是标题
思路二是用TableView的每一个section的row 0 做标题,其余的是数据,这种方法更为简单点,但是逻辑处理将会复杂一些
思路一经过一番实验后发现,问题主要表现在如何让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;
}
对cell自定义
if (indexPath.row == 0) {
//组名cell
if ([_datasouth[indexPath.section] count] == 1) {
//无分组的cell自定义
//等同于底层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;
}