欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用UItableview在iOS應(yīng)用開發(fā)中實現(xiàn)好友列表功能

 更新時間:2015年12月04日 09:25:23   作者:文頂頂  
這篇文章主要介紹了使用UItableview在iOS應(yīng)用開發(fā)中實現(xiàn)一個好友列表功能的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

基本實現(xiàn)
一、項目結(jié)構(gòu)和plist文件

201512491832922.png (989×495)

二、實現(xiàn)代碼

1.說明:

主控制器直接繼承UITableViewController

復(fù)制代碼 代碼如下:

//  YYViewController.h
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface YYViewController : UITableViewController

@end


在storyboard中進(jìn)行了關(guān)聯(lián)

201512492017809.png (1125×201)

2.代碼

數(shù)據(jù)模型部分:

YYQQGroupModel.h文件

復(fù)制代碼 代碼如下:

//
//  YYQQGroupModel.h
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface YYQQGroupModel : NSObject
/**
 *  名稱屬性
 */
@property(nonatomic,copy)NSString *name;
/**
 *  是否在線
 */
@property(nonatomic,copy)NSString *online;
/**
 *  好友列表
 */
@property(nonatomic,strong)NSArray *friends;

-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype) qqGroupModelWithDict:(NSDictionary *)dict;
@end


YYQQGroupModel.m文件
復(fù)制代碼 代碼如下:

//
//  YYQQGroupModel.m
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYQQGroupModel.h"
#import "YYFriendsModel.h"

@implementation YYQQGroupModel
-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self=[super init]) {
        //將字典轉(zhuǎn)換為模型
        [self setValuesForKeysWithDictionary:dict];
       
        //定義一個數(shù)組來保存轉(zhuǎn)換后的模型
        NSMutableArray *models=[NSMutableArray arrayWithCapacity:self.friends.count];
        for (NSDictionary *dict in self.friends) {
            YYFriendsModel *friends=[YYFriendsModel friendsWithDict:dict];
            [models addObject:friends];
        }
        _friends=[models copy];
    }
    return self;
}

+(instancetype)qqGroupModelWithDict:(NSDictionary *)dict
{
    return  [[self alloc]initWithDict:dict];
}
@end


YYFriendsModel.h文件
復(fù)制代碼 代碼如下:

//
//  YYFriendsModel.h
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface YYFriendsModel : NSObject
/**
 *  每個好友的名稱
 */
@property(nonatomic,copy)NSString *name;
/**
 *每個好友的頭像
 */
@property(nonatomic,copy)NSString *icon;
/**
 *  每個好友的個性簽名
 */
@property(nonatomic,copy)NSString *intro;
/**
 *  該好友是否是vip
 */
@property(nonatomic,assign,getter = isVip)BOOL vip;

-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)friendsWithDict:(NSDictionary *)dict;
@end


YYFriendsModel.m文件
復(fù)制代碼 代碼如下:

//
//  YYFriendsModel.m
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYFriendsModel.h"

@implementation YYFriendsModel
-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self=[super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+(instancetype)friendsWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}
@end


視圖部分

YYfriendCell.h文件

復(fù)制代碼 代碼如下:

//
//  YYfriendCell.h
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import <UIKit/UIKit.h>
@class YYFriendsModel;
@interface YYfriendCell : UITableViewCell

@property(nonatomic,strong)YYFriendsModel *friends;

+(instancetype)cellWithTableview:(UITableView *)tableView;
@end


YYfriendCell.m文件
復(fù)制代碼 代碼如下:

//
//  YYfriendCell.m
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYfriendCell.h"
#import "YYFriendsModel.h"
//私有擴(kuò)展
@interface YYfriendCell()


@end


復(fù)制代碼 代碼如下:

@implementation YYfriendCell

+(YYfriendCell *)cellWithTableview:(UITableView *)tableView
{
    static NSString *identifier=@"qq";
    YYfriendCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        //這里使用系統(tǒng)自帶的樣式
        cell=[[YYfriendCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        NSLog(@"創(chuàng)建一個cell");
    }
    return cell;
}

-(void)setFriends:(YYFriendsModel *)friends
{
    _friends=friends;
    //1.設(shè)置頭像
    self.imageView.image=[UIImage imageNamed:_friends.icon];
       //2.設(shè)置昵稱
    self.textLabel.text=_friends.name;
     //3.設(shè)置簡介
    self.detailTextLabel.text=_friends.intro;
 //判斷是否是會員
    /**
     *  這里有個注意點,如果不寫else設(shè)置為黑色,會怎么樣?
     */
    if (_friends.isVip) {
        [self.textLabel setTextColor:[UIColor redColor]];
    }else
    {
    [self.textLabel setTextColor:[UIColor blackColor]];
    }
}
@end


主控制器部分

YYViewController.m文件

復(fù)制代碼 代碼如下:

//
//  YYViewController.m
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"
#import "YYQQGroupModel.h"
#import "YYfriendCell.h"
#import "YYFriendsModel.h"

@interface YYViewController ()
/**
 *  用來保存所有的分組數(shù)據(jù)
 */
@property(nonatomic,strong)NSArray *groupFriends;
@end


復(fù)制代碼 代碼如下:

@implementation YYViewController
#pragma mark-懶加載
//1.先拿到數(shù)據(jù),實現(xiàn)懶加載
-(NSArray *)groupFriends
{
    if (_groupFriends==nil) {
        NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"friends.plist" ofType:nil];
        NSArray *arrayM=[NSArray arrayWithContentsOfFile:fullpath];
       
        NSMutableArray *models=[NSMutableArray arrayWithCapacity:arrayM.count];
        for (NSDictionary *dict in arrayM) {
            YYQQGroupModel *group=[YYQQGroupModel qqGroupModelWithDict:dict];
            [models addObject:group];
        }
        _groupFriends=[models copy];
    }
    return _groupFriends;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     self.tableView.sectionHeaderHeight = 100;
}

#pragma mark-  設(shè)置數(shù)據(jù)源
//返回多少組
//為什么這里不會智能提示?因為這些方法是uitableview協(xié)議里的,默認(rèn)并沒有遵守協(xié)議,讓主控制器類繼承uitableviewcontroller后,就已經(jīng)遵守了
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groupFriends.count;
}
//每組返回多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //取出對應(yīng)的組模型
    YYQQGroupModel *group=self.groupFriends[section];
    //返回對應(yīng)組中的好友數(shù)
    return group.friends.count;
}
//每組每行的內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.創(chuàng)建cell
    YYfriendCell *cell=[YYfriendCell cellWithTableview:tableView];

    //2.設(shè)置cell
    YYQQGroupModel *group=self.groupFriends[indexPath.section];
    YYFriendsModel *friends=group.friends[indexPath.row];
    cell.friends=friends;
    //3.返回一個cell
    return cell;
}


#pragma mark - 代理方法
// 當(dāng)一個分組標(biāo)題進(jìn)入視野的時候就會調(diào)用該方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //    1.創(chuàng)建頭部視圖
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor grayColor];
    //    2.返回頭部視圖
    return view;
}

//設(shè)置分組頭部標(biāo)題的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

#pragma mark  隱藏狀態(tài)欄
-(BOOL)prefersStatusBarHidden
{
    return YES;
}
@end


實現(xiàn)的簡陋效果:

201512492117194.png (316×503)

三、注意點

(1)設(shè)置頭部視圖的方法

201512492135800.png (790×404)

(2)在重寫set方法時,應(yīng)該考慮到回滾。

201512492152513.png (570×231)

更為復(fù)雜的實現(xiàn)
一、實現(xiàn)效果

201512492209267.png (317×497)201512492303290.png (318×501)

二、實現(xiàn)代碼

1.數(shù)據(jù)模型部分

 YYQQGroupModel.h文件

復(fù)制代碼 代碼如下:

//
//  YYQQGroupModel.h
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface YYQQGroupModel : NSObject
/**
 *  名稱屬性
 */
@property(nonatomic,copy)NSString *name;
/**
 *  是否在線
 */
@property(nonatomic,copy)NSString *online;
/**
 *  好友列表
 */
@property(nonatomic,strong)NSArray *friends;

//記錄當(dāng)前組是否要打開
@property(nonatomic,assign,getter = isOpen)BOOL open;

-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype) qqGroupModelWithDict:(NSDictionary *)dict;
@end


 YYQQGroupModel.m文件
復(fù)制代碼 代碼如下:

//
//  YYQQGroupModel.m
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYQQGroupModel.h"
#import "YYFriendsModel.h"

@implementation YYQQGroupModel
-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self=[super init]) {
        //將字典轉(zhuǎn)換為模型
        [self setValuesForKeysWithDictionary:dict];
       
        //定義一個數(shù)組來保存轉(zhuǎn)換后的模型
        NSMutableArray *models=[NSMutableArray arrayWithCapacity:self.friends.count];
        for (NSDictionary *dict in self.friends) {
            YYFriendsModel *friends=[YYFriendsModel friendsWithDict:dict];
            [models addObject:friends];
        }
        _friends=[models copy];
    }
    return self;
}

+(instancetype)qqGroupModelWithDict:(NSDictionary *)dict
{
    return  [[self alloc]initWithDict:dict];
}
@end


YYFriendsModel.h文件
復(fù)制代碼 代碼如下:

//
//  YYFriendsModel.h
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface YYFriendsModel : NSObject
/**
 *  每個好友的名稱
 */
@property(nonatomic,copy)NSString *name;
/**
 *每個好友的頭像
 */
@property(nonatomic,copy)NSString *icon;
/**
 *  每個好友的個性簽名
 */
@property(nonatomic,copy)NSString *intro;
/**
 *  該好友是否是vip
 */
@property(nonatomic,assign,getter = isVip)BOOL vip;

-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)friendsWithDict:(NSDictionary *)dict;
@end


YYFriendsModel.m文件
復(fù)制代碼 代碼如下:

//
//  YYFriendsModel.m
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYFriendsModel.h"

@implementation YYFriendsModel
-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self=[super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+(instancetype)friendsWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}
@end


2.視圖部分

YYfriendCell.h文件

復(fù)制代碼 代碼如下:

//
//  YYfriendCell.h
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import <UIKit/UIKit.h>
@class YYFriendsModel;
@interface YYfriendCell : UITableViewCell

@property(nonatomic,strong)YYFriendsModel *friends;

+(instancetype)cellWithTableview:(UITableView *)tableView;
@end


YYfriendCell.m文件
復(fù)制代碼 代碼如下:

//
//  YYfriendCell.m
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYfriendCell.h"
#import "YYFriendsModel.h"
//私有擴(kuò)展
@interface YYfriendCell()


@end


復(fù)制代碼 代碼如下:

@implementation YYfriendCell

+(YYfriendCell *)cellWithTableview:(UITableView *)tableView
{
    static NSString *identifier=@"qq";
    YYfriendCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        //這里使用系統(tǒng)自帶的樣式
        cell=[[YYfriendCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        NSLog(@"創(chuàng)建一個cell");
    }
    return cell;
}

-(void)setFriends:(YYFriendsModel *)friends
{
    _friends=friends;
    //1.設(shè)置頭像
    self.imageView.image=[UIImage imageNamed:_friends.icon];
       //2.設(shè)置昵稱
    self.textLabel.text=_friends.name;
   
     //3.設(shè)置簡介
    self.detailTextLabel.text=_friends.intro;
 //判斷是否是會員
   
    /**
     *  這里有個注意點,如果不寫else設(shè)置為黑色,會怎么樣?
     */
    if (_friends.isVip) {
        [self.textLabel setTextColor:[UIColor redColor]];
    }else
    {
    [self.textLabel setTextColor:[UIColor blackColor]];
    }
     //調(diào)整字體的大小
    self.textLabel.font=[UIFont systemFontOfSize:15.f];
    self.detailTextLabel.font=[UIFont systemFontOfSize:10.f];
}
@end


YYHeaderView.h文件
復(fù)制代碼 代碼如下:

//
//  YYHeaderView.h
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-6-1.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import <UIKit/UIKit.h>

@class YYQQGroupModel,YYHeaderView;

//商量一個協(xié)議
@protocol YYHeaderViewDelegate <NSObject>
-(void)headerViewDidClickHeaderView:(YYHeaderView *)headerView;
@end


復(fù)制代碼 代碼如下:

@interface YYHeaderView : UITableViewHeaderFooterView

@property(nonatomic,strong)YYQQGroupModel *group;
//提供一個類方法,創(chuàng)建一個頭部視圖
+(instancetype)headerWithTableView:(UITableView *)tableView;


//delegate遵守YYHeaderViewDelegate這個協(xié)議,可以使用協(xié)議中的方法
@property(nonatomic,weak)id<YYHeaderViewDelegate> delegate;
@end

YYHeaderView.m文件

//
//  YYHeaderView.m
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-6-1.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYHeaderView.h"
#import "YYQQGroupModel.h"

@interface YYHeaderView()
@property(nonatomic,strong)UIButton *btn;
@property(nonatomic,strong)UILabel *lab;
@end


復(fù)制代碼 代碼如下:

@implementation YYHeaderView


//創(chuàng)建一個自定義的頭部分組視圖
+(instancetype)headerWithTableView:(UITableView *)tableView
{
    static NSString *indentifier=@"header";
    //先到緩存池中去取數(shù)據(jù)
    YYHeaderView *headerview=[tableView dequeueReusableCellWithIdentifier:indentifier];
    //如果沒有,則自己創(chuàng)建
    if (headerview==nil) {
        headerview=[[YYHeaderView alloc]initWithReuseIdentifier:indentifier];
    }
    //返回一個頭部視圖
    return headerview;
}

#warning 注意在構(gòu)造方法中為控件設(shè)置的frame是無效的
-(id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    //初始化父類中的構(gòu)造方法
    if (self=[super initWithReuseIdentifier:reuseIdentifier]) {
        //創(chuàng)建一個按鈕
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        //設(shè)置按鈕的屬性
        //設(shè)置普通狀態(tài)下按鈕的背景圖片
        [btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
        //設(shè)置高亮狀態(tài)下按鈕的背景圖片
        [btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
       
        //設(shè)置按鈕上的小三角圖片
        [btn setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
        //設(shè)置按鈕上信息的對其方式為左對齊
        btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
        //設(shè)置小三角圖片的內(nèi)邊距
        btn.contentEdgeInsets=UIEdgeInsetsMake(0, 20, 0, 0);
        //設(shè)置按鈕上文字距離小三角圖片的距離
        btn.titleEdgeInsets=UIEdgeInsetsMake(0, 20, 0, 0);
        //設(shè)置按鈕上分組標(biāo)題的文本顏色(默認(rèn)是白色)
        //[btn setTintColor:[UIColor blackColor]];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //添加按鈕的點擊事件
        [btn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
       
        // 設(shè)置btn中的圖片不填充整個imageview
        btn.imageView.contentMode = UIViewContentModeCenter;
        // 超出范圍的圖片不要剪切
               // btn.imageView.clipsToBounds = NO;
        btn.imageView.layer.masksToBounds = NO;
       
        //把按鈕添加到視圖
        [self addSubview:btn];
        self.btn=btn;
       
        //創(chuàng)建一個lab
        UILabel *lab=[[UILabel alloc]init];
        //設(shè)置在線人數(shù)的對齊方式為右對齊
        lab.textAlignment=NSTextAlignmentRight;
        //設(shè)置在線人數(shù)的文本顏色為灰色
        lab.textColor=[UIColor grayColor];
        [self addSubview:lab];
        self.lab=lab;
    }
    return self;
}


-(void)btnOnclick:(UIButton *)btn
{
    NSLog(@"按鈕被點擊了");
    //修改模型的isopen屬性
    //1.修改模型數(shù)據(jù)
    self.group.open=!self.group.isOpen;
    //2.刷新表格
    //(刷新表格的功能由控制器完成,在這里可以設(shè)置一個代理),當(dāng)按鈕被點擊的時候,就通知代理對表格進(jìn)行刷新
    //通知代理
    if ([self.delegate respondsToSelector:@selector(headerViewDidClickHeaderView:)]) {
        [self.delegate headerViewDidClickHeaderView:self];
    }
}

//當(dāng)控件的frame值改變時,會自動調(diào)用該方法,故可以在該方法中設(shè)置控件的frame;
-(void)layoutSubviews
{
#warning 一定不要忘記調(diào)用父類的方法
    [super layoutSubviews];
    //設(shè)置按鈕的frame和頭部視圖一樣大小
    self.btn.frame=self.bounds;
   
    //設(shè)置lab的frame
    CGFloat padding=20;
    CGFloat labW=50;
    CGFloat labH=self.frame.size.height;
    CGFloat labY=0;
    CGFloat labX=self.frame.size.width-padding-labW;
    self.lab.frame=CGRectMake(labX, labY, labW, labH);
}

#pragma mark - 當(dāng)一個控件被添加到其它視圖上的時候會調(diào)用以下方法
// 已經(jīng)被添加到父視圖上的時候會調(diào)用
- (void)didMoveToSuperview
{
    NSLog(@"已經(jīng)添加到視圖了");
    // 在這個方法中就快要拿到最新的被添加到tableview上的頭部視圖修改它的圖片
    if (self.group.isOpen) {
        //讓小三角圖片向下旋轉(zhuǎn)
        self.btn.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    }
}

// 即將被添加到父視圖上的時候會調(diào)用
- (void)willMoveToSuperview:(UIView *)newSuperview
{
     NSLog(@"將要添加到視圖了");
}


//重寫get方法,設(shè)置數(shù)據(jù)
-(void)setGroup:(YYQQGroupModel *)group
{
    _group=group;
    //設(shè)置分組標(biāo)題

    //self.btn.titleLabel.text=_group.name;
    #warning 請注意在設(shè)置按鈕的文本時,一定要設(shè)置按鈕的狀態(tài),像上面這樣設(shè)置不會顯示
    [self.btn setTitle:_group.name forState:UIControlStateNormal];
    NSLog(@"%@",self.btn.titleLabel.text);
    //設(shè)置在線人數(shù)
    self.lab.text=[NSString stringWithFormat:@"%@/%d",_group.online,_group.friends.count];
}

@end


3.控制器部分

YYViewController.h文件

復(fù)制代碼 代碼如下:

//
//  YYViewController.h
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface YYViewController : UITableViewController

@end

YYViewController.m文件

//
//  YYViewController.m
//  02-QQ好友列表(基本數(shù)據(jù)的加載)
//
//  Created by apple on 14-5-31.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"
#import "YYQQGroupModel.h"
#import "YYfriendCell.h"
#import "YYFriendsModel.h"
#import "YYHeaderView.h"

@interface YYViewController ()<YYHeaderViewDelegate>
/**
 *  用來保存所有的分組數(shù)據(jù)
 */
@property(nonatomic,strong)NSArray *groupFriends;
@end


復(fù)制代碼 代碼如下:

@implementation YYViewController
#pragma mark-懶加載
//1.先拿到數(shù)據(jù),實現(xiàn)懶加載
-(NSArray *)groupFriends
{
    if (_groupFriends==nil) {
        NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"friends.plist" ofType:nil];
        NSArray *arrayM=[NSArray arrayWithContentsOfFile:fullpath];
       
        NSMutableArray *models=[NSMutableArray arrayWithCapacity:arrayM.count];
        for (NSDictionary *dict in arrayM) {
            YYQQGroupModel *group=[YYQQGroupModel qqGroupModelWithDict:dict];
            [models addObject:group];
        }
        _groupFriends=[models copy];
    }
    return _groupFriends;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     self.tableView.sectionHeaderHeight = 100;
       
}

#pragma mark-  設(shè)置數(shù)據(jù)源
//返回多少組
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groupFriends.count;
}
//每組返回多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    //取出對應(yīng)的組模型
        YYQQGroupModel *group=self.groupFriends[section];
//    //返回對應(yīng)組中的好友數(shù)
//    return group.friends.count;
   
    //在這里進(jìn)行判斷,如果該組收攏,那就返回0行,如果該組打開,就返回實際的行數(shù)
//    if (group.isOpen) {
//        return group.friends.count;
//    }else
//    {
//        return 0;
//    }
   
    if (group.isOpen) {
        // 代表要展開
        return group.friends.count;
    }else
    {
        // 代表要合攏
        return 0;
    }
}
//每組每行的內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.創(chuàng)建cell
    YYfriendCell *cell=[YYfriendCell cellWithTableview:tableView];

    //2.設(shè)置cell
    YYQQGroupModel *group=self.groupFriends[indexPath.section];
    YYFriendsModel *friends=group.friends[indexPath.row];
    cell.friends=friends;
    //3.返回一個cell
    return cell;
}


#pragma mark - 代理方法
// 當(dāng)一個分組標(biāo)題進(jìn)入視野的時候就會調(diào)用該方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//    //    1.創(chuàng)建頭部視圖
//    UIView *view = [[UIView alloc] init];
//    view.backgroundColor = [UIColor grayColor];
//    //    2.返回頭部視圖
//    return view;
   
    //創(chuàng)建自定義的頭部視圖
    YYHeaderView *headerview=[YYHeaderView headerWithTableView:tableView];
   
    //設(shè)置當(dāng)前控制器為代理
    headerview.delegate=self;
    //設(shè)置頭部視圖的數(shù)據(jù)
    YYQQGroupModel *groupmodel=self.groupFriends[section];
    headerview.group=groupmodel;
    //返回頭部視圖
    return headerview;
}


#pragma mark - YYHeaderViewDelegate
-(void)headerViewDidClickHeaderView:(YYHeaderView *)headerView
{
    //重新調(diào)用數(shù)據(jù)源的方法刷新數(shù)據(jù)
    [self.tableView reloadData];
}
//設(shè)置分組頭部標(biāo)題的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

#pragma mark  隱藏狀態(tài)欄
-(BOOL)prefersStatusBarHidden
{
    return YES;
}
@end


三、代碼說明

1.項目文件結(jié)構(gòu)

201512492325539.png (923×544)

2.注意點

(1)調(diào)整字體的大?。?nbsp;   self.textLabel.font=[UIFont systemFontOfSize:15.f];

(2)-(void)layoutSubviews方法。該方法在控件的frame被改變的時候就會調(diào)用,這個方法一般用于調(diào)整子控件的位置,注意一定要調(diào)用[super layoutSubviews];

(3)但凡在init方法中獲取到的frame都是0;

(4)如果控件不顯示,有以下一些排錯方法

a.frame為空(沒有設(shè)置frame)
b.hidden是否為YES
c.alpha<=0.1(透明度)
d.沒有添加到父控件中
e.查看父控件以上幾點
(5)請注意在設(shè)置按鈕的文本時,一定要設(shè)置按鈕的狀態(tài)

正確:[self.btn setTitle:_group.name forState:UIControlStateNormal];
錯誤: self.btn.titleLabel.text=_group.name;
(6)調(diào)用構(gòu)造方法時,一定要先初始化父類的方法,先判斷,再進(jìn)行自己屬性的初始化

self=[super initWithReuseIdentifier:reuseIdentifier]
if(self)
{
……
}
(7)當(dāng)一個控件被添加到其它視圖上的時候會調(diào)用以下方法
1) 已經(jīng)被添加到父視圖上的時候會調(diào)用- (void)didMoveToSuperview

2) 即將被添加到父視圖上的時候會調(diào)用- (void)willMoveToSuperview:(UIView *)newSuperview

(8)圖片填充知識

   1)設(shè)置btn中的圖片不填充整個imageview btn.imageView.contentMode = UIViewContentModeCenter;

   2)超出范圍的圖片不要剪切

  //btn.imageView.clipsToBounds = NO;

  btn.imageView.layer.masksToBounds = NO;

四、補(bǔ)充(代理)

設(shè)置代理的幾個步驟
(1)如果一個視圖中的某個按鈕被點擊了,這個時候需要去主控制器中刷新數(shù)據(jù)。有一種做法是,讓這個視圖擁有控制器這個屬性,然后當(dāng)按鈕被點擊的時候去利用該屬性去做刷新數(shù)據(jù)的操作。另一種做法是把控制器設(shè)置為這個視圖的代理,當(dāng)視圖中的某個按鈕被點擊的時候,通知它的代理(主控制器)去干刷新數(shù)據(jù)這件事。
(2)要成為代理是由條件的,有以下幾個步驟
1).雙方約定一個協(xié)議(代理協(xié)議,注意命名規(guī)范),在視圖中自定義一個協(xié)議,協(xié)議中提供一個方法。

復(fù)制代碼 代碼如下:

@protocol YYHeaderViewDelegate <NSObject>

-(void)headerViewDidClickHeaderView:(YYHeaderView *)headerView;

@end


2).在視圖中添加一個id類型的屬性變量,任何人只要遵守了約定協(xié)議的都可以成為它的代理。
//delegate遵守YYHeaderViewDelegate這個協(xié)議,可以使用協(xié)議中的方法
復(fù)制代碼 代碼如下:

@property(nonatomic,weak)id<YYHeaderViewDelegate> delegate;

3).在控制器中,遵守自定義的代理協(xié)議,就可以使用代理提供的方法,在這個方法中對數(shù)據(jù)進(jìn)行刷新。
復(fù)制代碼 代碼如下:

@interface YYViewController ()<YYHeaderViewDelegate>

-(void)headerViewDidClickHeaderView:(YYHeaderView *)headerView

{

    [self.tableView reloadData];

}


4).把控制器設(shè)置作為按鈕點擊事件的代理。
headerview.delegate=self;

相關(guān)文章

最新評論