【iOS干货:快速集成tableView折叠cell的小框架】

前言

之前看到网上有很多类似于QQ分组的cell折叠的效果,但是很少有封装好的。这里借鉴了网上的一些资料,尝试着封装了一个简单易的YUFoldingTableView,不用自己再去实现具体逻辑,可快速实现tableView的cell折叠效果,使用方式和UItableView差不多,这里提供了两种简单的样式

demo下载

点击下载demo源代码

demo效果演示

使用步骤

1.导入头文件,遵守协议

#import "ViewController.h"
#import "YUFoldingTableView.h"
@interface ViewController () <YUFoldingTableViewDelegate>
@property (nonatomic, weak) YUFoldingTableView *foldingTableView;
@end

2.创建YUFoldingTableView

YUFoldingTableView *foldingTableView = [[YUFoldingTableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64)];
_foldingTableView = foldingTableView;
[self.view addSubview:foldingTableView];
foldingTableView.foldingDelegate = self;

3.实现YUFoldingTableView的代理,用法和UItableView类似

#pragma mark - YUFoldingTableViewDelegate / required(必须实现的代理)
// 返回箭头的位置
- (YUFoldingSectionHeaderArrowPosition)perferedArrowPositionForYUFoldingTableView:(YUFoldingTableView *)yuTableView
{
    return YUFoldingSectionHeaderArrowPositionLeft;
}
- (NSInteger )numberOfSectionForYUFoldingTableView:(YUFoldingTableView *)yuTableView
{
    return 5;
}
- (NSInteger )yuFoldingTableView:(YUFoldingTableView *)yuTableView numberOfRowsInSection:(NSInteger )section
{
    return 3;
}
- (CGFloat )yuFoldingTableView:(YUFoldingTableView *)yuTableView heightForHeaderInSection:(NSInteger )section
{
    return 50;
}
- (CGFloat )yuFoldingTableView:(YUFoldingTableView *)yuTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (NSString *)yuFoldingTableView:(YUFoldingTableView *)yuTableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"Title %ld",section];
}
- (UITableViewCell *)yuFoldingTableView:(YUFoldingTableView *)yuTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [yuTableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Row %ld",indexPath.row];
    return cell;
}

- (void )yuFoldingTableView:(YUFoldingTableView *)yuTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [yuTableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - YUFoldingTableViewDelegate / optional (可选择实现的)
- (NSString *)yuFoldingTableView:(YUFoldingTableView *)yuTableView descriptionForHeaderInSection:(NSInteger )section
{
    return @"detailText";
}

demo下载

点击下载demo源代码

总结

使用还是很方便的吧,代理方法也基本按照UItableView的代理去写的。另外,代码里面肯定还有很多问题,希望各位大神能够指正,我会尽量去修改,谢谢。(PS:这并不完全是原创,是参考了网上很多资料写出来的😊)

posted @ 2017-03-15 14:18  timelyRain  阅读(3079)  评论(0编辑  收藏  举报