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

前言

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

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的代理去寫的。另外偎快,代碼里面肯定還有很多問(wèn)題冯乘,希望各位大神能夠指正,我會(huì)盡量去修改晒夹,謝謝裆馒。(PS:這并不完全是原創(chuàng),是參考了網(wǎng)上很多資料寫出來(lái)的)

以后會(huì)不定期分享一些干活丐怯,希望大家一起交流领追,求關(guān)注,謝謝啦响逢。??
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末绒窑,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子舔亭,更是在濱河造成了極大的恐慌些膨,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件钦铺,死亡現(xiàn)場(chǎng)離奇詭異订雾,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)矛洞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門洼哎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人沼本,你說(shuō)我怎么就攤上這事噩峦。” “怎么了抽兆?”我有些...
    開封第一講書人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵识补,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我辫红,道長(zhǎng)凭涂,這世上最難降的妖魔是什么祝辣? 我笑而不...
    開封第一講書人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮切油,結(jié)果婚禮上蝙斜,老公的妹妹穿的比我還像新娘。我一直安慰自己澎胡,他們只是感情好乍炉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著滤馍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪底循。 梳的紋絲不亂的頭發(fā)上巢株,一...
    開封第一講書人閱讀 51,301評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音熙涤,去河邊找鬼阁苞。 笑死,一個(gè)胖子當(dāng)著我的面吹牛祠挫,可吹牛的內(nèi)容都是我干的那槽。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼等舔,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼骚灸!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起慌植,我...
    開封第一講書人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤甚牲,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后蝶柿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體丈钙,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年交汤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了雏赦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡芙扎,死狀恐怖星岗,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情戒洼,我是刑警寧澤伍茄,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站施逾,受9級(jí)特大地震影響敷矫,放射性物質(zhì)發(fā)生泄漏例获。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一曹仗、第九天 我趴在偏房一處隱蔽的房頂上張望榨汤。 院中可真熱鬧,春花似錦怎茫、人聲如沸收壕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蜜宪。三九已至,卻和暖如春祥山,著一層夾襖步出監(jiān)牢的瞬間圃验,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工缝呕, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留澳窑,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓供常,卻偏偏與公主長(zhǎng)得像摊聋,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子栈暇,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容