iOS头条干货androidiOS开发攻城狮的集散地

TableView分组的简单实现

2016-06-22  本文已影响2275人  萌九

先说下实现的效果,用户点击某个分组,展示该分组的列表,在展示的同时,用户不能通过上下滑动来滑动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
上一篇 下一篇

猜你喜欢

热点阅读