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

前言

最近在簡書上收獲不少,也覺得有必要把自己整理的東西分享給大家。之前看到網(wǎng)上有很多類似于QQ分組的cell折疊的效果,但是很少有封裝好的。這里借鑒了網(wǎng)上的一些資料,嘗試著封裝了一個簡單易的YUFoldingTableView,不用自己再去實(shí)現(xiàn)具體邏輯,可快速實(shí)現(xiàn)tableView的cell折疊效果,使用方式和UItableView差不多,這里提供了兩種簡單的樣式

demo下載

點(diǎn)擊下載demo源代碼

demo效果

2016-08-25 11_10_09.gif

使用步驟

1.導(dǎo)入頭文件,遵守協(xié)議

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

2.創(chuàng)建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;
    // 可以設(shè)置cell默認(rèn)展開,不設(shè)置的話,默認(rèn)折疊
    foldingTableView.foldingState = YUFoldingSectionStateShow;

3.實(shí)現(xiàn)YUFoldingTableView的代理,用法和UItableView類似

#pragma mark - YUFoldingTableViewDelegate / required(必須實(shí)現(xiàn)的代理)
- (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 (可選擇實(shí)現(xiàn)的)
// 自定義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:@"標(biāo)題 - %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下載

點(diǎn)擊下載demo源代碼

總結(jié)

使用還是很方便的吧,代理方法也基本按照UItableView的代理去寫的。另外,代碼里面肯定還有很多問題,希望各位大神能夠指正,我會盡量去修改,謝謝。(PS:這并不完全是原創(chuàng),是參考了網(wǎng)上很多資料寫出來的)

以后會不定期分享一些干活,希望大家一起交流,求關(guān)注,謝謝啦。??
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容