美文网首页iOS 大神之路ios 程序猿FamilyUI
【iOS干货:快速集成tableView折叠cell的小框架】

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

作者: timelyRain | 来源:发表于2016-08-25 11:20 被阅读4238次

前言

最近在简书上收获不少,也觉得有必要把自己整理的东西分享给大家。之前看到网上有很多类似于QQ分组的cell折叠的效果,但是很少有封装好的。这里借鉴了网上的一些资料,尝试着封装了一个简单易的YUFoldingTableView,不用自己再去实现具体逻辑,可快速实现tableView的cell折叠效果,使用方式和UItableView差不多,这里提供了两种简单的样式

demo下载

点击下载demo源代码

demo效果

2016-08-25 11_10_09.gif

使用步骤

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

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

2.创建YUFoldingTableView

    self.automaticallyAdjustsScrollViewInsets = NO;
    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;
    // 可以设置cell默认展开,不设置的话,默认折叠
    foldingTableView.foldingState = YUFoldingSectionStateShow;

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

#pragma mark - YUFoldingTableViewDelegate / required(必须实现的代理)
- (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;
}
- (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;
}

#pragma mark - YUFoldingTableViewDelegate / optional (可选择实现的)
// 自定义sectionHeaderView
- (UIView *)yuFoldingTableView:(UITableView *)yuTableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *headerIdentifier = @"headerIdentifier";
    YUCustomHeaderView *headerFooterView = [yuTableView dequeueReusableHeaderFooterViewWithIdentifier:headerIdentifier];
    if (headerFooterView == nil) {
        headerFooterView = [[YUCustomHeaderView alloc] initWithReuseIdentifier:headerIdentifier];
    }
    headerFooterView.contentView.backgroundColor = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:0.2];
    headerFooterView.title = [NSString stringWithFormat:@"标题 - %ld", section];
    headerFooterView.descriptionText = [NSString stringWithFormat:@"自定义的sectionHeaderView - %ld", section];
    return headerFooterView;
}
- (NSString *)yuFoldingTableView:(YUFoldingTableView *)yuTableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"Title %ld",section];
}
// 返回箭头的位置
- (YUFoldingSectionHeaderArrowPosition)perferedArrowPositionForYUFoldingTableView:(YUFoldingTableView *)yuTableView
{
    return YUFoldingSectionHeaderArrowPositionLeft;
}

- (void )yuFoldingTableView:(YUFoldingTableView *)yuTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [yuTableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (NSString *)yuFoldingTableView:(YUFoldingTableView *)yuTableView descriptionForHeaderInSection:(NSInteger )section
{
    return @"detailText";
}

demo下载

点击下载demo源代码

总结

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

以后会不定期分享一些干活,希望大家一起交流,求关注,谢谢啦。😊

相关文章

网友评论

  • 陈凯歌:6楼提到的问题依然存在啊
  • 瞬间看见永远:if (indexPath.section == 0) {
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == 0) {
    cell.textLabel.text = @"测试";
    cell.imageView.image = [UIImage imageNamed:@"测试2"];
    }

    当我写出这样的判断,会发现在下面345section都会出现文字,请问是怎么回事
  • 7452b91c71fd:没有重用啊,老铁
    timelyRain:重用可以自己去做呀,老铁
  • 相百赛:怎么只有func yuFoldingTableView(_ yuTableView: YUFoldingTableView!, didSelectRowAt indexPath: IndexPath!) 这个方法 没有。didDeselect取消选中的,而且每次点击head都会把cell的选择属性重新加载了
  • 回味岁月:如何能获取到当前头部的打开或者折叠状态呢? 当自定义头部需要加箭头切换的时候 谢谢了
    回味岁月:@timelyRain 发现和我用UITableViewAutomaticDimension 这个自动获取行高有关,,,
    回味岁月:@timelyRain 多谢 刚试了试有了 不过我这边有个问题 就是我当前页面有2个section 首次从网络获取数据以后 只显示第一个sectionheader 点击之后第二个才会出现 不知道你碰到过没 我这会正在查原因在哪,,,有点蛋疼
    timelyRain:@回味岁月 你下载一下我最新的代码,YUFoldingTableView里面的statusArray存放着每个section的折叠状态
  • Easy_VO:*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndexedSubscript:]: index 14 beyond bounds [0 .. 5]'
    *** First throw call stack:
    [
    "0 CoreFoundation 0x0000000105c201e6 __exceptionPreprocess + 294",
    "1 libobjc.A.dylib 0x0000000104d68031 objc_exception_throw + 48",
    "2 CoreFoundation 0x0000000105c600bc _CFThrowFormattedException + 194",
    "3 CoreFoundation 0x0000000105c517b1 -[__NSArrayM objectAtIndexedSubscript:] + 177",
    "4 CoinBTC 0x000000010098fd48 -[YUFoldingTableView tableView:numberOfRowsInSection:] + 104",
    "5 UIKit 0x0000000102d37851 -[UITableView _numberOfRowsInSection:] + 62",
    "6 UIKit 0x0000000102fe6e5b -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2631",
    "7 UIKit 0x0000000102feb350 -[UITableViewRowData numberOfRows] + 95",
    "8 UIKit 0x0000000102d04df3 -[UITableView noteNumberOfRowsChanged] + 117",
    "9 UIKit 0x0000000102d04289 -[UITableView reloadData] + 1354",
    "10 CoinBTC 0x0000000100a9c89b -[

    会蹦啊老表
    timelyRain:@Easy_VO 下载最新的代码试试
    Easy_VO:刷新的时候
  • 30d430c75985:盆友,默认展开第一个section,怎么处理,要么全部打开,要么全部关闭,这个有点单一啊
    timelyRain:@____Hm 已添加,可设置foldingTableView.sectionStateArray = @[@"1", @"0", @"0"]; 指定行展开,“1” 表示展开
  • 重驹:老表,当cell无限多的时候,点击section收不回来了,咋回事
    timelyRain:@重驹 我看了啊,感觉还好吧,不清楚你说的具体情况
    重驹:@timelyRain 你试试每个section底下有一百个cell,然后全部打开,再收某一个,模拟器上就会消失掉cell,下拉一下界面又会恢复页面
    timelyRain:无限多? 方便的话发给我看看
  • 哭泣男孩儿:对大神我一向很敬重,谢谢大神
  • 07584e6d9774:group模式没数据。。
  • ROFL:当section及cell较多时,从从倒数第二个section开始展开,然后再展开最后一个section,会出现重复问题,不知道博主发现没?
    ChrisPaulss:恩恩是的!我也发现这个问题了 主要是在plus的屏幕的时候
  • 其实也没有:请问 sectionHeaderHeight 怎么根据sectionHeader内容 自动调整高度
    其实也没有:@timelyRain 🙏
    timelyRain:这个你得先把内容的高度计算好,然后在代理返回每个sectionHeader对应的高度就可以了
  • 暖色调iOS:请问怎么获取到header的点击事件
    timelyRain:@暖色调iOS 我刚刚改了一下,foldingTableView.foldingState = YUFoldingSectionStateShow;这样设置可以默认展开,之前一直没时间弄这个,不好意思。
    暖色调iOS:@timelyRain 那一楼问的。默认展开应该怎么处理。前几天自己改了下 然而没有成功:sweat:
    timelyRain:这个没有写代理,你可以自己改一下。。
  • 葵安i:能问一下 怎样 才能点击另一个header 会收起另一个header呢
  • 1b2cb54e2129:默认展开怎么处理
    1b2cb54e2129:@timelyRain 没事没事, 其实我搞好了, 一直忙, 忘了和你说一声
    timelyRain:我刚刚改了一下,foldingTableView.foldingState = YUFoldingSectionStateShow;这样设置可以默认展开,之前一直没时间弄这个,不好意思。

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

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