iOS開發(fā)-仿QQ好友列表展開/關(guān)閉

前言:前端時(shí)間小編的一個(gè)朋友要做一個(gè)類似QQ列表的展開與關(guān)閉的效果,于是小編在閑暇之余給他寫了一個(gè)demo议街,本著好東西大家一起來分享的精神泽谨,趁著今天有時(shí)間小編把這個(gè)demo在此與大家一起共享,希望能夠幫到有需要的朋友特漩。

原理:使用UITableView列表形式吧雹,對(duì)sectionHeaderView添加tap手勢(shì),點(diǎn)擊展開/關(guān)閉該section通過對(duì)每個(gè)section記錄一個(gè)是否展開的BOOL值來實(shí)現(xiàn)拾稳。

小編沒有做很細(xì)化的效果吮炕,先看看簡(jiǎn)單效果圖如下:


未展開.png

展開.png

下面來介紹代碼的實(shí)現(xiàn):
由于沒有網(wǎng)絡(luò)數(shù)據(jù),小編只能自己偽造了一些假數(shù)據(jù)访得,大家在使用的時(shí)候替換掉就可以了龙亲。

#import "ViewController.h"
#import "QQFriendCell.h"

#define Cell_QQFriend   @"Cell_QQFriend_ID"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;
//總數(shù)據(jù)
@property (nonatomic, strong) NSMutableArray * dataArr;
//每組的標(biāo)題
@property (nonatomic, strong) NSMutableArray * sectionArr;
//存儲(chǔ)是否展開的BOOL值
@property (nonatomic, strong) NSMutableArray * boolArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"好友列表";
    [self loadData];
    [self addSubviews];
    [self makeConstraintsForUI];
}

//加載數(shù)據(jù)
- (void)loadData {
    
    NSArray * secArr = @[@"我的好友", @"小學(xué)同學(xué)", @"初中同學(xué)", @"高中同學(xué)", @"大學(xué)同學(xué)"];
    NSArray * rowsArr = @[@(12), @(10), @(15), @(13), @(22)];
    
    for (int i = 0; i < secArr.count; i++) {
        
        NSMutableArray * friendArr = [[NSMutableArray alloc] init];
        for (int j = 0; j < [rowsArr[i] intValue]; j++) {
            
            [friendArr addObject:@(j)];
        }
        [self.dataArr addObject:friendArr];
        [self.sectionArr addObject:secArr[i]];
        [self.boolArr addObject:@NO];
    }
}

#pragma mark - add subviews

- (void)addSubviews {
    
    [self.view addSubview:self.tableView];
}

#pragma mark - make constraints

- (void)makeConstraintsForUI {
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(Screen_Width, Screen_Height));
        make.left.mas_equalTo(@0);
        make.top.mas_equalTo(@0);
    }];
}

#pragma mark - tableView delegate and dataSource

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //判斷是否展開,如果未展開則返回0
    if ([self.boolArr[section] boolValue] == NO) {
        
        return 0;
    }else {
        
        return [self.dataArr[section] count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QQFriendCell * cell = [tableView dequeueReusableCellWithIdentifier:Cell_QQFriend forIndexPath:indexPath];
    
    if (indexPath.row < [self.dataArr[indexPath.section] count]) {
        //這里可以傳入請(qǐng)求的數(shù)據(jù)悍抑,此方法可以根據(jù)自己的需求做更改
        [cell configCellWithData:nil row:indexPath.row];
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    //創(chuàng)建header的view
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 50)];
    headerView.tag = 2016 + section;
    headerView.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1];
    
    //添加imageview
    UIImageView * iv = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 20, 20)];
    
    //三目運(yùn)算選擇展開或者閉合時(shí)候的圖標(biāo)
    iv.image = [_boolArr[section] boolValue] ? [UIImage imageNamed:@"buddy_header_arrow_down"] : [UIImage imageNamed:@"buddy_header_arrow_right"];
    [headerView addSubview:iv];
    
    //添加標(biāo)題label
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(35, 0, Screen_Width - 100, 50)];
    label.text = self.sectionArr[section];
    [headerView addSubview:label];
    
    //添加分組人數(shù)和在線人數(shù)顯示的label
    UILabel * labelR = [[UILabel alloc] initWithFrame:CGRectMake(Screen_Width - 60, 0, 60, 50)];
    labelR.textAlignment = NSTextAlignmentCenter;
    //這里小編把在線人數(shù)全部設(shè)置成了0鳄炉,可以根據(jù)需求更改
    labelR.text = [NSString stringWithFormat:@"%d/%lu", 0, [self.dataArr[section] count]];
    [headerView addSubview:labelR];
    
    //添加輕扣手勢(shì)
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
    [headerView addGestureRecognizer:tap];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 70;
}

#pragma mark - action
- (void)tapGR:(UITapGestureRecognizer *)tapGR {
    
    //獲取section
    NSInteger section = tapGR.view.tag - 2016;
    //判斷改變bool值
    if ([_boolArr[section] boolValue] == YES) {
        [_boolArr replaceObjectAtIndex:section withObject:@NO];
    }else {
        [_boolArr replaceObjectAtIndex:section withObject:@YES];
    }
    //刷新某個(gè)section
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
}

#pragma mark - setter and getter

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[QQFriendCell class] forCellReuseIdentifier:Cell_QQFriend];
        //下面這行代碼可以避免分組都沒展開時(shí),空白處出現(xiàn)的橫線
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

- (NSMutableArray *)dataArr {
    
    if (!_dataArr) {
        
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

- (NSMutableArray *)sectionArr {
    
    if (!_sectionArr) {
        
        _sectionArr = [[NSMutableArray alloc] init];
    }
    return _sectionArr;
}

- (NSMutableArray *)boolArr {
    
    if (!_boolArr) {
        
        _boolArr = [[NSMutableArray alloc] init];
    }
    return _boolArr;
}

@end

上邊是ViewController里邊的代碼搜骡,小編對(duì)Cell也進(jìn)行了簡(jiǎn)單的定制拂盯,同時(shí)SectionHeaderView在此寫的有些復(fù)雜,代碼比較多记靡,其實(shí)可以單獨(dú)拉出來進(jìn)行定制谈竿,定制之后再次調(diào)用會(huì)看起來清爽很多,但是小編時(shí)間有限就沒有定制摸吠,只是對(duì)Cell進(jìn)行了簡(jiǎn)單的定制空凸,在此只看.m中的代碼即可:

#import "QQFriendCell.h"

@interface QQFriendCell ()
//頭像
@property (nonatomic, strong) UIImageView * image_icon;
//昵稱
@property (nonatomic, strong) UILabel * lab_nickname;
//簽名
@property (nonatomic, strong) UILabel * lab_signature;
@end
@implementation QQFriendCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self addSubviews];
    }
    return self;
}

#pragma mark - add subviews

- (void)addSubviews {
    
    [self addSubview:self.image_icon];
    [self addSubview:self.lab_nickname];
    [self addSubview:self.lab_signature];
}

#pragma mark - layout subviews

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    [_image_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(50, 50));
        make.left.mas_equalTo(@15);
        make.top.mas_equalTo(@10);
    }];
    
    [_lab_nickname mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_image_icon.mas_top);
        make.left.mas_equalTo(_image_icon.mas_right).with.offset(15);
        make.right.mas_equalTo(@-15);
        make.height.mas_equalTo(@21);
    }];
    
    [_lab_signature mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(_image_icon.mas_bottom);
        make.left.mas_equalTo(_lab_nickname.mas_left);
        make.right.mas_equalTo(_lab_nickname.mas_right);
        make.height.mas_equalTo(@21);
    }];
}

#pragma mark - config cell
//此方法為外漏方法,可以根據(jù)自己的需求更改
- (void)configCellWithData:(NSDictionary *)dic row:(NSInteger)row {
    
    _image_icon.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1.0];
    _lab_nickname.text = [NSString stringWithFormat:@"好友%lu", row + 1];
    _lab_signature.text = [NSString stringWithFormat:@"這是好友%lu的簽名", row + 1];
}

#pragma mark - setter and getter

- (UIImageView *)image_icon {
    
    if (!_image_icon) {
        
        _image_icon = [[UIImageView alloc] init];
        _image_icon.contentMode = UIViewContentModeScaleAspectFill;
        _image_icon.layer.cornerRadius = 25.0;
        _image_icon.layer.masksToBounds = YES;
    }
    return _image_icon;
}

- (UILabel *)lab_nickname {
    
    if (!_lab_nickname) {
        
        _lab_nickname = [[UILabel alloc] init];
        _lab_nickname.font = [UIFont systemFontOfSize:18];
    }
    return _lab_nickname;
}
@end

看到這里就結(jié)束了寸痢,是不是很簡(jiǎn)單呢呀洲,其實(shí)只要知道點(diǎn)擊展開/關(guān)閉的原理,相信大家都可以寫出效果來的。

總結(jié)一下此demo用到的幾個(gè)關(guān)鍵的點(diǎn):
1道逗、需要一個(gè)存儲(chǔ)各個(gè)分組是否展開的BOOL值的數(shù)組兵罢;
2、tableView刷新某個(gè)Section的動(dòng)態(tài)方法

demo下載地址:https://github.com/guorenhao/TencentQQ.git

最后滓窍,小編還是希望此文能夠幫助到有需要的朋友們卖词,愿同是程序猿的我們能夠共同學(xué)習(xí)進(jìn)步,在開發(fā)的道路上越走越遠(yuǎn)贰您!謝謝坏平!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市锦亦,隨后出現(xiàn)的幾起案子舶替,更是在濱河造成了極大的恐慌,老刑警劉巖杠园,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件顾瞪,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡抛蚁,警方通過查閱死者的電腦和手機(jī)陈醒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來瞧甩,“玉大人钉跷,你說我怎么就攤上這事《且荩” “怎么了爷辙?”我有些...
    開封第一講書人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)朦促。 經(jīng)常有香客問我膝晾,道長(zhǎng),這世上最難降的妖魔是什么务冕? 我笑而不...
    開封第一講書人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任血当,我火速辦了婚禮,結(jié)果婚禮上禀忆,老公的妹妹穿的比我還像新娘臊旭。我一直安慰自己,他們只是感情好箩退,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開白布巍扛。 她就那樣靜靜地躺著,像睡著了一般乏德。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,682評(píng)論 1 312
  • 那天喊括,我揣著相機(jī)與錄音胧瓜,去河邊找鬼。 笑死郑什,一個(gè)胖子當(dāng)著我的面吹牛府喳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蘑拯,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼钝满,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了申窘?” 一聲冷哼從身側(cè)響起弯蚜,我...
    開封第一講書人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎剃法,沒想到半個(gè)月后碎捺,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贷洲,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年收厨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片优构。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡诵叁,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出钦椭,到底是詐尸還是另有隱情拧额,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布玉凯,位于F島的核電站势腮,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏漫仆。R本人自食惡果不足惜捎拯,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望盲厌。 院中可真熱鬧署照,春花似錦、人聲如沸吗浩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽懂扼。三九已至禁荸,卻和暖如春右蒲,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背赶熟。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工瑰妄, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人映砖。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓间坐,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親邑退。 傳聞我的和親對(duì)象是個(gè)殘疾皇子竹宋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件地技、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,124評(píng)論 4 61
  • 前幾天束析,薛之謙轉(zhuǎn)發(fā)的北京地鐵粗暴事件的視頻鄙煤,想必大家都看了离唬。 孰是孰非凿歼,公眾也已經(jīng)有了比較公允的說法。 我只是看到...
    硝煙閱讀 587評(píng)論 0 0
  • 2017年4月15日,中原小語工作室聯(lián)盟第三次活動(dòng)如期舉行彬伦,河南省著名教師劉娟娟老師做客許昌實(shí)驗(yàn)小學(xué)滔悉,為實(shí)驗(yàn)小學(xué)教...
    李艷平名師工作室成員梅琳閱讀 690評(píng)論 0 0
  • 哎呦,不好肚子好痛单绑,得趕緊沖進(jìn)廁所……賢哥怎么辦回官?此時(shí)他還在懷里抱著,最近這家伙特別粘人搂橙,估計(jì)好難脫身歉提,家里水管又...
    燕紀(jì)事閱讀 485評(píng)論 9 1
  • 老歌OT你的背包 填詞:鄺鑒萍 作曲:蔡政勛 街角的老屋 你我笑過也悲哭 如今返 找不到三嬸七叔 歲月匆匆 不問 ...
    鄺鑒萍閱讀 245評(píng)論 0 2