用TableView展示QQ好友列表的效果

本文主要講述如何利用UITableView來實現(xiàn)類似于QQ聯(lián)系人列表的界面展示遭殉。首先說一下創(chuàng)建的文件:

文件類目圖片

上圖可知情连,demo中我創(chuàng)建了一個自定義的cell以及一個自定義的基于UITableViewHeaderFooterView的MyHeaderView规丽,用來顯示分組折疊時候的樣式和內(nèi)容鹦倚。
接下來按照文件來詳細(xì)說一下具體的代碼內(nèi)容:
1栅屏、自定義cell文件:FriendsListCell
主要寫了一個imageView用來展示聯(lián)系人頭像戈次、一個label來顯示聯(lián)系人昵稱。


效果圖1

2隘世、自定義view文件:MyHeaderView
在.h文件中主要寫了一個代理去實現(xiàn)點擊分組視圖時的響應(yīng)事件可柿。

#import <UIKit/UIKit.h>

@protocol MyHeaderViewDelegate <NSObject>

- (void)whenHeaderViewDidTapWithTag:(NSInteger)headerTag;

@end

@interface MyHeaderView : UITableViewHeaderFooterView

@property (nonatomic ,strong) UILabel *label;

@property (nonatomic ,weak) id <MyHeaderViewDelegate> headerDelegate;

@end

在.m文件中就是實現(xiàn)控件的搭建而已。我就直接展示代碼了(此處丙者、請忽略本寶寶粗糙的女漢子審美复斥,我們重點看效果)

#import "MyHeaderView.h"

@implementation MyHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        [self createContentView];
    }
    
    return self;
}

- (void)createContentView
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 30, 30)];
    imageView.backgroundColor = [UIColor redColor];
    [self addSubview:imageView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 5, 100, 30)];
    [self addSubview:label];
    self.label = label;
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 20 - 50, 5, 50, 30)];
    label2.text = @"4/9";
    label2.textColor = [UIColor grayColor];
    label2.textAlignment = NSTextAlignmentRight;
    [self addSubview:label2];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHeaderView)];
    [self addGestureRecognizer:tapGes];
}

- (void)tapHeaderView
{
    if ([self.headerDelegate respondsToSelector:@selector(whenHeaderViewDidTapWithTag:)]) {
        [self.headerDelegate whenHeaderViewDidTapWithTag:self.tag];
    }
}
@end

3、接下來就是在VC中實現(xiàn):
"鑒于代碼注釋還是很詳細(xì)的械媒,此處就不再贅述目锭,看代碼。纷捞。痢虹。"

#import "ViewController.h"

#import "FriendsListCell.h"
#import "MyHeaderView.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,MyHeaderViewDelegate>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSArray *dataArray;

@property (nonatomic ,strong) NSArray *sectionArray;

@property (nonatomic ,strong) NSMutableArray *stateArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupAllArrays];
    
    [self setupTableView];
    
}

- (void)setupAllArrays
{
    // 初始化數(shù)據(jù) (這里是我隨便寫的一些,具體可根據(jù)數(shù)據(jù)請求結(jié)果來寫)
    _sectionArray = @[@"我的好友",@"大學(xué)同學(xué)",@"我的同事",@"高中同學(xué)",@"陌生人"];
    
    NSArray *data = @[@"好友001",@"好友002",@"好友003",@"好友004",@"好友005",@"好友006",@"好友007",@"好友008",@"好友009"];
    
    _dataArray = [NSArray arrayWithObjects:data,data,data,data,data,nil];
    
    _stateArray = [NSMutableArray array];
    
    for (int i = 0; i < _dataArray.count; i++) {
        
        [_stateArray addObject:@"0"];
    }
}

- (void)setupTableView
{
    // 設(shè)置tableView的style為: UITableViewStylePlain
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 50;
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

// 返回分組數(shù)量(_sectionArray.count)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sectionArray.count;
}

// 自定義分組頭視圖(viewForHeaderInSection)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // 聯(lián)系人分組組名稱
    // 點擊可控制列表折疊或展開
    MyHeaderView *headerview = [[MyHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
    headerview.headerDelegate = self;
    headerview.tag = (section + 10);
    headerview.label.text = _sectionArray[section];
    
    return headerview;
}

// 設(shè)置分組視圖的高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

// 根據(jù)展開或者折疊狀態(tài)設(shè)置行數(shù)--row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_stateArray[section] isEqualToString:@"1"]) {
        // 展開狀態(tài)
        NSArray *array = [_dataArray objectAtIndex:section];
        return array.count;
    }else
    {
        return 0;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FriendsListCell *cell = [FriendsListCell cellWithTableView:tableView];
    cell.nameLabel.text = _dataArray[indexPath.section][indexPath.row];
    return cell;
}

// headerView 的代理方法主儡,點擊控制展開/折疊
- (void)whenHeaderViewDidTapWithTag:(NSInteger)headerTag
{
    // 判斷目前分組是否展開
    if ([_stateArray[headerTag - 10] isEqualToString:@"1"])
    {
        [_stateArray replaceObjectAtIndex:(headerTag - 10) withObject:@"0"];
        
    }else
    {
        [_stateArray replaceObjectAtIndex:(headerTag - 10) withObject:@"1"];
    }
    
    // 刷新tableView的數(shù)據(jù)
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:(headerTag - 10)] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"第%ld組 -- 第%ld行",indexPath.section,indexPath.row);
}

@end
效果圖2

希望我的文章能對大家有所幫助奖唯。康撒米噠~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末糜值,一起剝皮案震驚了整個濱河市丰捷,隨后出現(xiàn)的幾起案子坯墨,更是在濱河造成了極大的恐慌,老刑警劉巖瓢阴,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件畅蹂,死亡現(xiàn)場離奇詭異,居然都是意外死亡荣恐,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門累贤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來叠穆,“玉大人,你說我怎么就攤上這事臼膏∨鸨唬” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵渗磅,是天一觀的道長嚷硫。 經(jīng)常有香客問我,道長始鱼,這世上最難降的妖魔是什么仔掸? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮医清,結(jié)果婚禮上起暮,老公的妹妹穿的比我還像新娘。我一直安慰自己会烙,他們只是感情好负懦,可當(dāng)我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著柏腻,像睡著了一般纸厉。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上五嫂,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天颗品,我揣著相機與錄音,去河邊找鬼贫导。 笑死抛猫,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的孩灯。 我是一名探鬼主播闺金,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼峰档!你這毒婦竟也來了败匹?” 一聲冷哼從身側(cè)響起寨昙,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎掀亩,沒想到半個月后舔哪,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡槽棍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年捉蚤,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片炼七。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡缆巧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出豌拙,到底是詐尸還是另有隱情陕悬,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布按傅,位于F島的核電站捉超,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏唯绍。R本人自食惡果不足惜拼岳,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望推捐。 院中可真熱鬧裂问,春花似錦、人聲如沸牛柒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽皮壁。三九已至椭更,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蛾魄,已是汗流浹背虑瀑。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留滴须,地道東北人舌狗。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像扔水,于是被迫代替她去往敵國和親痛侍。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,512評論 2 359

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