TableView分组的简单实现

先说下实现的效果,用户点击某个分组,展示该分组的列表,在展示的同时,用户不能通过上下滑动来滑动TableView,在用户点击分组的时候,该分组会移动贴近导航栏。类似这样的效果。思路和上一篇的UICollectionView分组类似,下面是效果图:

qh_2.gif

实现的代码全贴上去,有什么疑问可以在评论里说

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSInteger openSection;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *aArr;
@property (nonatomic, strong) NSMutableArray *bArr;
@property (nonatomic, strong) NSMutableArray *cArr;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, assign) BOOL isDo;
@end

@implementation ViewController
static NSString *const cellID = @"cell";
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"分组";
    _isDo = NO;
    openSection = -1;
    [self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
    button.backgroundColor = [UIColor cyanColor];
    [button setTitle:[NSString stringWithFormat:@"%ld", section] forState:UIControlStateNormal];
    [button addTarget:self action:sel_registerName("doOpen:") forControlEvents:UIControlEventTouchUpInside];
    button.tag = section + 1000;
    return button;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}
- (void)doOpen:(UIButton *)sender {
    if (openSection == sender.tag - 1000) {
        _isDo = !_isDo;
        [self.tableView reloadData];
    } else {
        _isDo = NO;
        openSection = sender.tag - 1000;
        [self.tableView reloadData];
    }
    if (!_isDo) {
        self.tableView.scrollEnabled = NO; // 打开状态 不能滑动
        [self.tableView setContentOffset:CGPointMake(0, openSection * 44 - 64) animated:YES];
    } else {
        self.tableView.scrollEnabled = YES; // 关闭状态 能够滑动
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == openSection) {
        if (_isDo) { // YES 为打开过
            return 0;
        } // 没打开过
        return [self.dataArr[openSection] count];
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.textLabel.text = [_dataArr[indexPath.section][indexPath.row] stringValue];
    return cell;
}

- (NSMutableArray *)aArr {
    if (!_aArr) {
        _aArr = [NSMutableArray arrayWithObjects:@111,@222,@333,@444,@555,@666,@777, nil];
    }
    return _aArr;
}
- (NSMutableArray *)bArr {
    if (!_bArr) {
        _bArr = [NSMutableArray arrayWithObjects:@2222,@4444,@6666,@8888,@1111, nil];
    }
    return _bArr;
}
- (NSMutableArray *)cArr {
    if (!_cArr) {
        _cArr = [NSMutableArray arrayWithObjects:@33333,@55555,@77777,@99999,@11111,@22222,@33333, nil];
    }
    return _cArr;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray arrayWithObjects:self.aArr,self.bArr,self.cArr, nil];
    }
    return _dataArr;
}

该分组效果是在某个App下看到的,于是就根据那个效果敲了这个分组给大家分享下。

qh_1.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,261评论 4 61
  • 汪家平:上海虹海出租的一名普通双班车驾驶员。平时给人的印象就是憨厚,朴实,少言寡语。进公司十多年了,一直...
    虹海张波阅读 1,919评论 0 5
  • 生活好像确实告诉了自己很多道理,明白了很多。不走寻常路也好 活着就好。 时间总会告诉你,全部所有都会变得平淡无奇,...
    刘小九九三阅读 169评论 0 0
  • 多好的命题作文,我确实有相当多的体会。 这句话也许有两层意思。第一,亲密关系中的人是需要成长的。第二,成长不是孤立...
    燕英俊阅读 516评论 5 0