可展開和收縮的UITableView

項(xiàng)目中需要做一個類似于QQ通訊錄的效果侦高,即可展開和收縮的UITableView嫉柴,主要是區(qū)模型通過一個標(biāo)記參數(shù)進(jìn)行UITableView的開合。

效果如下:

demo.gif

主控制器代碼如下:

#import "ViewController.h"
#import "MySectionHeaderView.h"
#import "MyCell.h"
#import "MyClass.h"
#import "MyStudent.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *datas;
@property (strong, nonatomic) NSMutableArray *classes;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:@"MyCellID"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MySectionHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"MySectionHeaderViewID"];
    self.tableView.tableFooterView = [UIView new];
    self.classes = [NSMutableArray array];
    self.datas = @[    @{@"className" : @"Aitna",
                         @"students"  : @[@{@"name" : @"Y.X.M."},
                                          @{@"name" : @"Leif"},
                                          @{@"name" : @"Lennon"},
                                          @{@"name" : @"Lambert"},
                                          @{@"name" : @"Jerome"},
                                          @{@"name" : @"Isidore"}]},
                       @{@"className" : @"Melete",
                         @"students"  : @[@{@"name" : @"Merle"},
                                          @{@"name" : @"Paddy"},
                                          @{@"name" : @"Perry"},
                                          @{@"name" : @"Philip"}]},
                       @{@"className" : @"Aoede",
                         @"students"  : @[@{@"name" : @"Verne"},
                                          @{@"name" : @"Vincent"},
                                          @{@"name" : @"Walter"},
                                          @{@"name" : @"Zachary"}]},
                       @{@"className" : @"Dione",
                         @"students"  : @[@{@"name" : @"Timothy"},
                                          @{@"name" : @"Roderick"},
                                          @{@"name" : @"Quentin"},
                                          @{@"name" : @"Paddy"}]},
                       @{@"className" : @"Adanos",
                         @"students"  : @[@{@"name" : @"Mortimer"},
                                           @{@"name" : @"Michael"},
                                           @{@"name" : @"Kevin"},
                                          @{@"name" : @"Jeremy"}]}];
    
    /** 處理數(shù)據(jù)奉呛,得到所有班級 */
    for (NSDictionary *class in self.datas) {
        
        MyClass *new_class = [MyClass new];
        new_class.class_name = class[@"className"];
        NSArray *students = class[@"students"];
        NSMutableArray *new_students = [NSMutableArray array];
        for (NSDictionary *student in students) {
            MyStudent *new_student = [MyStudent new];
            new_student.student_name = student[@"name"];
            [new_students addObject:new_student];
        }
        new_class.students = new_students;
        
        [self.classes addObject:new_class];
    }
    
}

#pragma mark - UITableViewDelegate DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.classes.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MyClass *class = self.classes[section];
    return class.open ? class.students.count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCellID" forIndexPath:indexPath];
    MyClass *class = self.classes[indexPath.section];
    cell.student = class.students[indexPath.row];
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MySectionHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MySectionHeaderViewID"];
    MyClass *class = self.classes[section];
    header.class_model = class;
    header.headViewClick = ^(MyClass *class_model) {
        /** 根據(jù)open參數(shù)進(jìn)行tableView的開合*/
        if (class.open) {
            
            NSMutableArray *indexPaths = [NSMutableArray array];
            for (int i = 0; i < class.students.count; i++) {
                [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:section]];
            }
            [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
            
        }else{
            NSMutableArray *indexPaths = [NSMutableArray array];
            for (int i = 0; i < class.students.count; i++) {
                [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:section]];
            }
            [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
        }
    };
    return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

GitHub

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末计螺,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子瞧壮,更是在濱河造成了極大的恐慌登馒,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件咆槽,死亡現(xiàn)場離奇詭異陈轿,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進(jìn)店門济欢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赠堵,“玉大人,你說我怎么就攤上這事法褥∶0龋” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵半等,是天一觀的道長揍愁。 經(jīng)常有香客問我,道長杀饵,這世上最難降的妖魔是什么莽囤? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮切距,結(jié)果婚禮上朽缎,老公的妹妹穿的比我還像新娘。我一直安慰自己谜悟,他們只是感情好话肖,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著葡幸,像睡著了一般最筒。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蔚叨,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天床蜘,我揣著相機(jī)與錄音,去河邊找鬼蔑水。 笑死邢锯,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的搀别。 我是一名探鬼主播丹擎,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼领曼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蛮穿,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤庶骄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后践磅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體单刁,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了羔飞。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肺樟。...
    茶點(diǎn)故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖逻淌,靈堂內(nèi)的尸體忽然破棺而出么伯,到底是詐尸還是另有隱情,我是刑警寧澤卡儒,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布田柔,位于F島的核電站,受9級特大地震影響骨望,放射性物質(zhì)發(fā)生泄漏硬爆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一擎鸠、第九天 我趴在偏房一處隱蔽的房頂上張望缀磕。 院中可真熱鬧,春花似錦劣光、人聲如沸袜蚕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽廷没。三九已至,卻和暖如春垂寥,著一層夾襖步出監(jiān)牢的瞬間颠黎,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工滞项, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留狭归,地道東北人。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓文判,卻偏偏與公主長得像过椎,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子戏仓,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評論 2 354

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