模仿QQ聯(lián)系人界面的折疊tableView

效果圖

screenshot.gif

原理

每個(gè)分組是一個(gè)UITableViewHeaderFooterView

@class FoldedTableViewHeaderFooterViewModel;

typedef void(^DidSelectBlock)(BOOL isExpanded);

@interface FoldedTableViewHeaderFooterView : UITableViewHeaderFooterView

@property (nonatomic, strong) UIImageView *arrowImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) UIView *topLineView;
@property (nonatomic, strong) UIView *bottomLineView;

- (void)setupWithModel:(FoldedTableViewHeaderFooterViewModel *)model
               section:(NSInteger)section
        didSelectBlock:(DidSelectBlock)block;

@end

每個(gè)分組有一個(gè)數(shù)據(jù)層viewModel

@class FoldedTableViewCellModel;

@interface FoldedTableViewHeaderFooterViewModel : NSObject

@property (nonatomic, assign, getter=isExpanded) BOOL expanded;///< 是否展開
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *number;
@property (nonatomic, strong) NSArray<FoldedTableViewCellModel *> *cellModelArray;

@end

expanded判斷分組是否展開,cellModelArray存儲(chǔ)當(dāng)前分組的聯(lián)系人數(shù)據(jù)

每個(gè)聯(lián)系人是一個(gè)UITableViewCell

@class FoldedTableViewCellModel;

@interface FoldedTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *leftImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIView *bottomLineView;
@property (nonatomic, strong) CALayer *imageViewMaskLayer;///< 離線頭像蒙版

- (void)setupWithModel:(FoldedTableViewCellModel *)model;

@end

每個(gè)聯(lián)系人有一個(gè)數(shù)據(jù)層cellModel

@interface FoldedTableViewCellModel : NSObject

@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *content;
@property (nonatomic, assign, getter=isOnline) BOOL online;///< 是否在線

@end

然后在tableView的方法里面簡單的設(shè)置

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    FoldedTableViewHeaderFooterViewModel *model = self.sectionData[section];
    return model.isExpanded ? model.cellModelArray.count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FoldedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FoldedTableViewCell" forIndexPath:indexPath];
    FoldedTableViewHeaderFooterViewModel *viewModel = self.sectionData[indexPath.section];
    FoldedTableViewCellModel *cellModel = viewModel.cellModelArray[indexPath.row];
    [cell setupWithModel:cellModel];
    if (indexPath.row == viewModel.cellModelArray.count - 1) {
        cell.bottomLineView.hidden = YES;
    }else {
        cell.bottomLineView.hidden = NO;
    }
    return cell;
}

Demo

https://github.com/guanzhendong/FoldedTableView

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 前言 由于最近兩個(gè)多月,筆者正和小伙伴們忙于對(duì)公司新項(xiàng)目的開發(fā),筆者主要負(fù)責(zé)項(xiàng)目整體架構(gòu)的搭建以及功能模塊的分工。...
    CoderMikeHe閱讀 27,358評(píng)論 74 270
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,297評(píng)論 3 38
  • 一 講一個(gè)不熟的朋友,92年的,在十八線的老家做著一份沒有社保,工資撐死2K5的工作。愛吹牛皮,我們當(dāng)?shù)卦挵汛蹬7Q...
    王啊白閱讀 395評(píng)論 0 0
  • 品牌:愛馬仕香調(diào):花香果香調(diào)前調(diào):橙子 茶葉中調(diào):桂花 小蒼蘭后調(diào):皮革 杏屬性:中性香調(diào)香師:Jean-Clau...
    削削削削小蘿卜閱讀 5,152評(píng)論 4 1
  • 周末一回婆家,笑笑就興奮地跑來跑去,一會(huì)兒抓著奶奶說,麻麻回來了,你看。一會(huì)兒又拉著爺爺說,麻麻回來了噢。進(jìn)入臘月...
    啾啾fing閱讀 276評(píng)論 0 0

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