TableView 下拉demo

今天給大家分享一個tableView 下拉效果的小demo萍虽。
首先轴脐,創(chuàng)建我們需要的類债热,兩個model:一個大model植捎,用來裝載分區(qū)頭,一個小model阳柔,裝載每個分組中的好友信息。起類名的時候需要見名知意蚓峦,如下圖:


需要創(chuàng)建的類

看一下plist文件中的數(shù)據(jù)舌剂,如何進行解析:


BigModel裝載的對應(yīng)數(shù)據(jù)

SmallModel裝載的數(shù)據(jù)

在BigModel中聲明對應(yīng)的key值屬性,還要聲明一個bool類型暑椰,用于后面的狀態(tài)判斷:
//
//  BigModel.h
//  UI_QQListProject
//
//  Created by 奇二世界 on 16/6/23.
//  Copyright ? 2016年 YMN. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BigModel : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *online;
@property (nonatomic, strong) NSMutableArray *friends;

// 判斷點擊分區(qū)頭下拉還是收起好友
@property (nonatomic, assign) BOOL isOpen;
@end

在SmallModel中聲明對應(yīng)的key值屬性:

//
//  SmallModel.h
//  UI_QQListProject
//
//  Created by 奇二世界 on 16/6/23.
//  Copyright ? 2016年 YMN. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface SmallModel : NSObject

@property (nonatomic, strong) NSString *icon;
@property (nonatomic, strong) NSString *intro;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSNumber *vip;
@end

在兩個model 的 .m 文件中要寫 setValue forUndefinedKey 這個方法霍转,以防出現(xiàn)未定義的key:

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}

我是直接在 ViewController 寫的,導(dǎo)入頭文件一汽,定義一個tableView 鋪放界面避消、聲明一個 modelArray 裝載數(shù)據(jù):

//
//  ViewController.m
//  UI_QQListProject
//
//  Created by 奇二世界 on 16/6/23.
//  Copyright ? 2016年 YMN. All rights reserved.
//

#import "ViewController.h"
#import "BigModel.h"
#import "SmallModel.h"

// 把屏幕的寬、高定義成宏定義召夹,方便調(diào)用
#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, strong) UITableView *tableV;

@property (nonatomic, strong) NSMutableArray *modelArray;


@end

@implementation ViewController

// modelArray 懶加載岩喷、解析數(shù)據(jù)
- (NSMutableArray *)modelArray
{
    if (!_modelArray)
    {
        _modelArray = [NSMutableArray array];
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"friends" ofType:@"plist"];
        NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path];
         // 解析大model:分組
        for (NSDictionary *dic in array)
        {
            BigModel *bigModel = [[BigModel alloc] init];
            [bigModel setValuesForKeysWithDictionary:dic];
            
            // 解析小model:對應(yīng)的好友信息
            NSMutableArray *friendsArr = dic[@"friends"];
            // !!! 一定要將裝載小model的數(shù)組bigModel.friends初始化
            bigModel.friends = [NSMutableArray array];
            for (NSDictionary *dic1 in friendsArr)
            {
                SmallModel *smallModel = [[SmallModel alloc] init];
                [smallModel setValuesForKeysWithDictionary:dic1];
                // 把小model裝入大model的好友數(shù)組中
                [bigModel.friends addObject:smallModel];
            }
            // 將裝入小model信息的大model裝入數(shù)組中
            [self.modelArray addObject:bigModel];
        }
    }
    return _modelArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) style:(UITableViewStylePlain)];
    self.tableV.dataSource = self;
    self.tableV.delegate = self;
    
    [self.view addSubview:self.tableV];
}

#pragma mark -- 協(xié)議中的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.modelArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    BigModel *bigModel = self.modelArray[section];
    // 如果分區(qū)頭是下拉狀態(tài),返回好友個數(shù)
    if (bigModel.isOpen)
    {
        // 返回大數(shù)組中friends數(shù)組中的每個分區(qū)好友個數(shù)
        return bigModel.friends.count;
    }
    // 否則监憎,分區(qū)頭是收起狀態(tài)纱意,cell則不顯示,行數(shù)為0
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
    }
    
    BigModel *bigModel = self.modelArray[indexPath.section];
    SmallModel *smallModel = bigModel.friends[indexPath.row];
    cell.textLabel.text = smallModel.name;
    cell.detailTextLabel.text = smallModel.intro;
    cell.imageView.image = [UIImage imageNamed:smallModel.icon];
    
    return cell;
}

// tableView 的分區(qū)頭
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 50)];
    view.tag = section + 100; // 點擊手勢時使用tag值判斷點擊的是哪個分區(qū)頭
    [self.view addSubview:view]; // 一定要將自定義的分區(qū)頭添加到根視圖上
    
    BigModel *model = self.modelArray[section];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 10, 10)];
    imageV.image = [UIImage imageNamed:@"buddy_header_arrow"];
    [view addSubview:imageV];
    
    // 判斷下拉狀態(tài)還是收起狀態(tài)鲸阔,來決定箭頭圖片旋轉(zhuǎn)度數(shù):
    imageV.transform = model.isOpen ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0);
   
    UILabel *groupNameL = [[UILabel alloc] initWithFrame:CGRectMake(35, 0, 150, 50)];
    groupNameL.text = model.name;
    [view addSubview:groupNameL];
    
    UILabel *numberL = [[UILabel alloc] initWithFrame:CGRectMake(ScreenW - 50, 0, 50, 50)];
    numberL.text = [NSString stringWithFormat:@"%@/%ld", model.online, model.friends.count];
    [view addSubview:numberL];
    
    // 分區(qū)頭之前的線
    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 1)];
    lineV.backgroundColor = [UIColor grayColor];
    [view addSubview:lineV]; // 或者把分區(qū)尾的高度設(shè)置成1偷霉,實現(xiàn)相同的效果
    
    // 給分區(qū)頭添加一個手勢
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [view addGestureRecognizer:tap];
    
    return view;

}

// 返回分區(qū)頭的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

#pragma mark -- 手勢的點擊方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    UIView *view = tap.view;
   NSInteger index = view.tag - 100;
    
    BigModel *model = self.modelArray[index];
    model.isOpen = !model.isOpen;
    [self.tableV reloadData]; // 一定要刷新UI
}

@end

以下是運行之后的效果圖:
注意看分組前面的小箭頭的狀態(tài)迄委,代碼中有詳細說明


收起的效果.

下拉效果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市类少,隨后出現(xiàn)的幾起案子叙身,更是在濱河造成了極大的恐慌,老刑警劉巖硫狞,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件信轿,死亡現(xiàn)場離奇詭異,居然都是意外死亡妓忍,警方通過查閱死者的電腦和手機虏两,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來世剖,“玉大人定罢,你說我怎么就攤上這事∨蕴保” “怎么了祖凫?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長酬凳。 經(jīng)常有香客問我惠况,道長,這世上最難降的妖魔是什么宁仔? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任稠屠,我火速辦了婚禮,結(jié)果婚禮上翎苫,老公的妹妹穿的比我還像新娘权埠。我一直安慰自己,他們只是感情好煎谍,可當(dāng)我...
    茶點故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布攘蔽。 她就那樣靜靜地躺著,像睡著了一般呐粘。 火紅的嫁衣襯著肌膚如雪满俗。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天作岖,我揣著相機與錄音唆垃,去河邊找鬼。 笑死痘儡,一個胖子當(dāng)著我的面吹牛降盹,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼蓄坏,長吁一口氣:“原來是場噩夢啊……” “哼价捧!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起涡戳,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤结蟋,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后渔彰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嵌屎,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年恍涂,在試婚紗的時候發(fā)現(xiàn)自己被綠了宝惰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,769評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡再沧,死狀恐怖尼夺,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情炒瘸,我是刑警寧澤淤堵,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站顷扩,受9級特大地震影響拐邪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜隘截,卻給世界環(huán)境...
    茶點故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一扎阶、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧婶芭,春花似錦乘陪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽贱勃。三九已至井赌,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間贵扰,已是汗流浹背仇穗。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留戚绕,地道東北人纹坐。 一個月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像舞丛,于是被迫代替她去往敵國和親耘子。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,781評論 2 361

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

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)氓癌,斷路器番舆,智...
    卡卡羅2017閱讀 134,714評論 18 139
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件捍歪、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,123評論 4 61
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件户辱,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,067評論 3 38
  • ———— 上午與師兄通完電話糙臼,甚感莫名荒疏庐镐,心意莫可名狀,無所寄著变逃,無處著力必逆,無得抓握。不想做事韧献,又百無聊賴末患,手邊...
    蓮心竹韻閱讀 448評論 0 2
  • 生意的本質(zhì)是賺錢渊啰,合作的本質(zhì)是雙贏探橱,只有雙贏了才可以合作長久,愉快的合作才能愉快的賺錢嘛绘证,對不對隧膏。本質(zhì)解決完了,下...
    佩奇的快樂生活閱讀 837評論 0 0