TableView分组的简单实现

作者: 萌九 | 来源:发表于2016-06-22 10:43 被阅读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

相关文章

网友评论

  • 4305824b6977:1234abcd
  • 三岁就喜欢学习:使用bool数组会更简单一点
  • 嗨_我是大鑫啊:不错,思路很清晰。。。追问下,三级列表怎么个思路。??
    嗨_我是大鑫啊:@萌九 谢谢!
    萌九:@BigXin 不过那个Cell你点击之后 如果是isTwoOpen是打开状态的话,你在普通Cell下面添加的视图你要建一个类方便点,并且那个类要声明点击事件的协议就可以在第三级列表中实现点击操作了。
    萌九:@BigXin 第一个是section分区头部对吧,第二级是Cell,然后在声明一个BOOL属性isTwoOpen来判断点击Cell之后的打开状态, 点击cell的后,再在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 方法中判断isTwoOpen状态返回不同高度 打开状态高度设置Cell正常状态的两倍,再在cell下面添加你所说的第三级的列表。

本文标题:TableView分组的简单实现

本文链接:https://www.haomeiwen.com/subject/lfzgdttx.html