欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

使用UItableview在iOS应用开发中实现好友列表功能

程序员文章站 2023-08-31 14:36:19
基本实现 一、项目结构和plist文件 二、实现代码 1.说明: 主控制器直接继承uitableviewcontroller 复制代码 代码如下: //...

基本实现
一、项目结构和plist文件

使用UItableview在iOS应用开发中实现好友列表功能

二、实现代码

1.说明:

主控制器直接继承uitableviewcontroller

复制代码 代码如下:

//  yyviewcontroller.h
//  02-qq好友列表(基本数据的加载)
//
//  created by apple on 14-5-31.
//  copyright (c) 2014年 itcase. all rights reserved.
//

#import <uikit/uikit.h>

@interface yyviewcontroller : uitableviewcontroller

@end


在storyboard中进行了关联

使用UItableview在iOS应用开发中实现好友列表功能

2.代码

数据模型部分:

yyqqgroupmodel.h文件

复制代码 代码如下:

//
//  yyqqgroupmodel.h
//  02-qq好友列表(基本数据的加载)
//
//  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文件
复制代码 代码如下:

//
//  yyqqgroupmodel.m
//  02-qq好友列表(基本数据的加载)
//
//  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]) {
        //将字典转换为模型
        [self setvaluesforkeyswithdictionary:dict];
       
        //定义一个数组来保存转换后的模型
        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文件
复制代码 代码如下:

//
//  yyfriendsmodel.h
//  02-qq好友列表(基本数据的加载)
//
//  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文件
复制代码 代码如下:

//
//  yyfriendsmodel.m
//  02-qq好友列表(基本数据的加载)
//
//  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文件

复制代码 代码如下:

//
//  yyfriendcell.h
//  02-qq好友列表(基本数据的加载)
//
//  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文件
复制代码 代码如下:

//
//  yyfriendcell.m
//  02-qq好友列表(基本数据的加载)
//
//  created by apple on 14-5-31.
//  copyright (c) 2014年 itcase. all rights reserved.
//

#import "yyfriendcell.h"
#import "yyfriendsmodel.h"
//私有扩展
@interface yyfriendcell()


@end


复制代码 代码如下:

@implementation yyfriendcell

+(yyfriendcell *)cellwithtableview:(uitableview *)tableview
{
    static nsstring *identifier=@"qq";
    yyfriendcell *cell=[tableview dequeuereusablecellwithidentifier:identifier];
    if (cell==nil) {
        //这里使用系统自带的样式
        cell=[[yyfriendcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:identifier];
        nslog(@"创建一个cell");
    }
    return cell;
}

-(void)setfriends:(yyfriendsmodel *)friends
{
    _friends=friends;
    //1.设置头像
    self.imageview.image=[uiimage imagenamed:_friends.icon];
       //2.设置昵称
    self.textlabel.text=_friends.name;
     //3.设置简介
    self.detailtextlabel.text=_friends.intro;
 //判断是否是会员
    /**
     *  这里有个注意点,如果不写else设置为黑色,会怎么样?
     */
    if (_friends.isvip) {
        [self.textlabel settextcolor:[uicolor redcolor]];
    }else
    {
    [self.textlabel settextcolor:[uicolor blackcolor]];
    }
}
@end


主控制器部分

yyviewcontroller.m文件

复制代码 代码如下:

//
//  yyviewcontroller.m
//  02-qq好友列表(基本数据的加载)
//
//  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 ()
/**
 *  用来保存所有的分组数据
 */
@property(nonatomic,strong)nsarray *groupfriends;
@end


复制代码 代码如下:

@implementation yyviewcontroller
#pragma mark-懒加载
//1.先拿到数据,实现懒加载
-(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-  设置数据源
//返回多少组
//为什么这里不会智能提示?因为这些方法是uitableview协议里的,默认并没有遵守协议,让主控制器类继承uitableviewcontroller后,就已经遵守了
-(nsinteger)numberofsectionsintableview:(uitableview *)tableview
{
    return self.groupfriends.count;
}
//每组返回多少行
-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section
{
    //取出对应的组模型
    yyqqgroupmodel *group=self.groupfriends[section];
    //返回对应组中的好友数
    return group.friends.count;
}
//每组每行的内容
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
    //1.创建cell
    yyfriendcell *cell=[yyfriendcell cellwithtableview:tableview];

    //2.设置cell
    yyqqgroupmodel *group=self.groupfriends[indexpath.section];
    yyfriendsmodel *friends=group.friends[indexpath.row];
    cell.friends=friends;
    //3.返回一个cell
    return cell;
}


#pragma mark - 代理方法
// 当一个分组标题进入视野的时候就会调用该方法
- (uiview *)tableview:(uitableview *)tableview viewforheaderinsection:(nsinteger)section
{
    //    1.创建头部视图
    uiview *view = [[uiview alloc] init];
    view.backgroundcolor = [uicolor graycolor];
    //    2.返回头部视图
    return view;
}

//设置分组头部标题的高度
-(cgfloat)tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section
{
    return 44;
}

#pragma mark  隐藏状态栏
-(bool)prefersstatusbarhidden
{
    return yes;
}
@end


实现的简陋效果:

使用UItableview在iOS应用开发中实现好友列表功能

三、注意点

(1)设置头部视图的方法

使用UItableview在iOS应用开发中实现好友列表功能

(2)在重写set方法时,应该考虑到回滚。

使用UItableview在iOS应用开发中实现好友列表功能

更为复杂的实现
一、实现效果

使用UItableview在iOS应用开发中实现好友列表功能使用UItableview在iOS应用开发中实现好友列表功能

二、实现代码

1.数据模型部分

 yyqqgroupmodel.h文件

复制代码 代码如下:

//
//  yyqqgroupmodel.h
//  02-qq好友列表(基本数据的加载)
//
//  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;

//记录当前组是否要打开
@property(nonatomic,assign,getter = isopen)bool open;

-(instancetype)initwithdict:(nsdictionary *)dict;
+(instancetype) qqgroupmodelwithdict:(nsdictionary *)dict;
@end


 yyqqgroupmodel.m文件
复制代码 代码如下:

//
//  yyqqgroupmodel.m
//  02-qq好友列表(基本数据的加载)
//
//  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]) {
        //将字典转换为模型
        [self setvaluesforkeyswithdictionary:dict];
       
        //定义一个数组来保存转换后的模型
        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文件
复制代码 代码如下:

//
//  yyfriendsmodel.h
//  02-qq好友列表(基本数据的加载)
//
//  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文件
复制代码 代码如下:

//
//  yyfriendsmodel.m
//  02-qq好友列表(基本数据的加载)
//
//  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文件

复制代码 代码如下:

//
//  yyfriendcell.h
//  02-qq好友列表(基本数据的加载)
//
//  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文件
复制代码 代码如下:

//
//  yyfriendcell.m
//  02-qq好友列表(基本数据的加载)
//
//  created by apple on 14-5-31.
//  copyright (c) 2014年 itcase. all rights reserved.
//

#import "yyfriendcell.h"
#import "yyfriendsmodel.h"
//私有扩展
@interface yyfriendcell()


@end


复制代码 代码如下:

@implementation yyfriendcell

+(yyfriendcell *)cellwithtableview:(uitableview *)tableview
{
    static nsstring *identifier=@"qq";
    yyfriendcell *cell=[tableview dequeuereusablecellwithidentifier:identifier];
    if (cell==nil) {
        //这里使用系统自带的样式
        cell=[[yyfriendcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:identifier];
        nslog(@"创建一个cell");
    }
    return cell;
}

-(void)setfriends:(yyfriendsmodel *)friends
{
    _friends=friends;
    //1.设置头像
    self.imageview.image=[uiimage imagenamed:_friends.icon];
       //2.设置昵称
    self.textlabel.text=_friends.name;
   
     //3.设置简介
    self.detailtextlabel.text=_friends.intro;
 //判断是否是会员
   
    /**
     *  这里有个注意点,如果不写else设置为黑色,会怎么样?
     */
    if (_friends.isvip) {
        [self.textlabel settextcolor:[uicolor redcolor]];
    }else
    {
    [self.textlabel settextcolor:[uicolor blackcolor]];
    }
     //调整字体的大小
    self.textlabel.font=[uifont systemfontofsize:15.f];
    self.detailtextlabel.font=[uifont systemfontofsize:10.f];
}
@end


yyheaderview.h文件
复制代码 代码如下:

//
//  yyheaderview.h
//  02-qq好友列表(基本数据的加载)
//
//  created by apple on 14-6-1.
//  copyright (c) 2014年 itcase. all rights reserved.
//

#import <uikit/uikit.h>

@class yyqqgroupmodel,yyheaderview;

//商量一个协议
@protocol yyheaderviewdelegate <nsobject>
-(void)headerviewdidclickheaderview:(yyheaderview *)headerview;
@end


复制代码 代码如下:

@interface yyheaderview : uitableviewheaderfooterview

@property(nonatomic,strong)yyqqgroupmodel *group;
//提供一个类方法,创建一个头部视图
+(instancetype)headerwithtableview:(uitableview *)tableview;


//delegate遵守yyheaderviewdelegate这个协议,可以使用协议中的方法
@property(nonatomic,weak)id<yyheaderviewdelegate> delegate;
@end

yyheaderview.m文件

//
//  yyheaderview.m
//  02-qq好友列表(基本数据的加载)
//
//  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


复制代码 代码如下:

@implementation yyheaderview


//创建一个自定义的头部分组视图
+(instancetype)headerwithtableview:(uitableview *)tableview
{
    static nsstring *indentifier=@"header";
    //先到缓存池中去取数据
    yyheaderview *headerview=[tableview dequeuereusablecellwithidentifier:indentifier];
    //如果没有,则自己创建
    if (headerview==nil) {
        headerview=[[yyheaderview alloc]initwithreuseidentifier:indentifier];
    }
    //返回一个头部视图
    return headerview;
}

#warning 注意在构造方法中为控件设置的frame是无效的
-(id)initwithreuseidentifier:(nsstring *)reuseidentifier
{
    //初始化父类中的构造方法
    if (self=[super initwithreuseidentifier:reuseidentifier]) {
        //创建一个按钮
        uibutton *btn=[uibutton buttonwithtype:uibuttontypecustom];
        //设置按钮的属性
        //设置普通状态下按钮的背景图片
        [btn setbackgroundimage:[uiimage imagenamed:@"buddy_header_bg"] forstate:uicontrolstatenormal];
        //设置高亮状态下按钮的背景图片
        [btn setbackgroundimage:[uiimage imagenamed:@"buddy_header_bg_highlighted"] forstate:uicontrolstatehighlighted];
       
        //设置按钮上的小三角图片
        [btn setimage:[uiimage imagenamed:@"buddy_header_arrow"] forstate:uicontrolstatenormal];
        //设置按钮上信息的对其方式为左对齐
        btn.contenthorizontalalignment=uicontrolcontenthorizontalalignmentleft;
        //设置小三角图片的内边距
        btn.contentedgeinsets=uiedgeinsetsmake(0, 20, 0, 0);
        //设置按钮上文字距离小三角图片的距离
        btn.titleedgeinsets=uiedgeinsetsmake(0, 20, 0, 0);
        //设置按钮上分组标题的文本颜色(默认是白色)
        //[btn settintcolor:[uicolor blackcolor]];
        [btn settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];
        //添加按钮的点击事件
        [btn addtarget:self action:@selector(btnonclick:) forcontrolevents:uicontroleventtouchupinside];
       
        // 设置btn中的图片不填充整个imageview
        btn.imageview.contentmode = uiviewcontentmodecenter;
        // 超出范围的图片不要剪切
               // btn.imageview.clipstobounds = no;
        btn.imageview.layer.maskstobounds = no;
       
        //把按钮添加到视图
        [self addsubview:btn];
        self.btn=btn;
       
        //创建一个lab
        uilabel *lab=[[uilabel alloc]init];
        //设置在线人数的对齐方式为右对齐
        lab.textalignment=nstextalignmentright;
        //设置在线人数的文本颜色为灰色
        lab.textcolor=[uicolor graycolor];
        [self addsubview:lab];
        self.lab=lab;
    }
    return self;
}


-(void)btnonclick:(uibutton *)btn
{
    nslog(@"按钮被点击了");
    //修改模型的isopen属性
    //1.修改模型数据
    self.group.open=!self.group.isopen;
    //2.刷新表格
    //(刷新表格的功能由控制器完成,在这里可以设置一个代理),当按钮被点击的时候,就通知代理对表格进行刷新
    //通知代理
    if ([self.delegate respondstoselector:@selector(headerviewdidclickheaderview:)]) {
        [self.delegate headerviewdidclickheaderview:self];
    }
}

//当控件的frame值改变时,会自动调用该方法,故可以在该方法中设置控件的frame;
-(void)layoutsubviews
{
#warning 一定不要忘记调用父类的方法
    [super layoutsubviews];
    //设置按钮的frame和头部视图一样大小
    self.btn.frame=self.bounds;
   
    //设置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 - 当一个控件被添加到其它视图上的时候会调用以下方法
// 已经被添加到父视图上的时候会调用
- (void)didmovetosuperview
{
    nslog(@"已经添加到视图了");
    // 在这个方法中就快要拿到最新的被添加到tableview上的头部视图修改它的图片
    if (self.group.isopen) {
        //让小三角图片向下旋转
        self.btn.imageview.transform = cgaffinetransformmakerotation(m_pi_2);
    }
}

// 即将被添加到父视图上的时候会调用
- (void)willmovetosuperview:(uiview *)newsuperview
{
     nslog(@"将要添加到视图了");
}


//重写get方法,设置数据
-(void)setgroup:(yyqqgroupmodel *)group
{
    _group=group;
    //设置分组标题

    //self.btn.titlelabel.text=_group.name;
    #warning 请注意在设置按钮的文本时,一定要设置按钮的状态,像上面这样设置不会显示
    [self.btn settitle:_group.name forstate:uicontrolstatenormal];
    nslog(@"%@",self.btn.titlelabel.text);
    //设置在线人数
    self.lab.text=[nsstring stringwithformat:@"%@/%d",_group.online,_group.friends.count];
}

@end


3.控制器部分

yyviewcontroller.h文件

复制代码 代码如下:

//
//  yyviewcontroller.h
//  02-qq好友列表(基本数据的加载)
//
//  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好友列表(基本数据的加载)
//
//  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>
/**
 *  用来保存所有的分组数据
 */
@property(nonatomic,strong)nsarray *groupfriends;
@end


复制代码 代码如下:

@implementation yyviewcontroller
#pragma mark-懒加载
//1.先拿到数据,实现懒加载
-(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-  设置数据源
//返回多少组
-(nsinteger)numberofsectionsintableview:(uitableview *)tableview
{
    return self.groupfriends.count;
}
//每组返回多少行
-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section
{
//    //取出对应的组模型
        yyqqgroupmodel *group=self.groupfriends[section];
//    //返回对应组中的好友数
//    return group.friends.count;
   
    //在这里进行判断,如果该组收拢,那就返回0行,如果该组打开,就返回实际的行数
//    if (group.isopen) {
//        return group.friends.count;
//    }else
//    {
//        return 0;
//    }
   
    if (group.isopen) {
        // 代表要展开
        return group.friends.count;
    }else
    {
        // 代表要合拢
        return 0;
    }
}
//每组每行的内容
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
    //1.创建cell
    yyfriendcell *cell=[yyfriendcell cellwithtableview:tableview];

    //2.设置cell
    yyqqgroupmodel *group=self.groupfriends[indexpath.section];
    yyfriendsmodel *friends=group.friends[indexpath.row];
    cell.friends=friends;
    //3.返回一个cell
    return cell;
}


#pragma mark - 代理方法
// 当一个分组标题进入视野的时候就会调用该方法
- (uiview *)tableview:(uitableview *)tableview viewforheaderinsection:(nsinteger)section
{
//    //    1.创建头部视图
//    uiview *view = [[uiview alloc] init];
//    view.backgroundcolor = [uicolor graycolor];
//    //    2.返回头部视图
//    return view;
   
    //创建自定义的头部视图
    yyheaderview *headerview=[yyheaderview headerwithtableview:tableview];
   
    //设置当前控制器为代理
    headerview.delegate=self;
    //设置头部视图的数据
    yyqqgroupmodel *groupmodel=self.groupfriends[section];
    headerview.group=groupmodel;
    //返回头部视图
    return headerview;
}


#pragma mark - yyheaderviewdelegate
-(void)headerviewdidclickheaderview:(yyheaderview *)headerview
{
    //重新调用数据源的方法刷新数据
    [self.tableview reloaddata];
}
//设置分组头部标题的高度
-(cgfloat)tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section
{
    return 30;
}

#pragma mark  隐藏状态栏
-(bool)prefersstatusbarhidden
{
    return yes;
}
@end


三、代码说明

1.项目文件结构

使用UItableview在iOS应用开发中实现好友列表功能

2.注意点

(1)调整字体的大小:    self.textlabel.font=[uifont systemfontofsize:15.f];

(2)-(void)layoutsubviews方法。该方法在控件的frame被改变的时候就会调用,这个方法一般用于调整子控件的位置,注意一定要调用[super layoutsubviews];

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

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

a.frame为空(没有设置frame)
b.hidden是否为yes
c.alpha<=0.1(透明度)
d.没有添加到父控件中
e.查看父控件以上几点
(5)请注意在设置按钮的文本时,一定要设置按钮的状态

正确:[self.btn settitle:_group.name forstate:uicontrolstatenormal];
错误: self.btn.titlelabel.text=_group.name;
(6)调用构造方法时,一定要先初始化父类的方法,先判断,再进行自己属性的初始化

self=[super initwithreuseidentifier:reuseidentifier]
if(self)
{
……
}
(7)当一个控件被添加到其它视图上的时候会调用以下方法
1) 已经被添加到父视图上的时候会调用- (void)didmovetosuperview

2) 即将被添加到父视图上的时候会调用- (void)willmovetosuperview:(uiview *)newsuperview

(8)图片填充知识

   1)设置btn中的图片不填充整个imageview btn.imageview.contentmode = uiviewcontentmodecenter;

   2)超出范围的图片不要剪切

  //btn.imageview.clipstobounds = no;

  btn.imageview.layer.maskstobounds = no;

四、补充(代理)

设置代理的几个步骤
(1)如果一个视图中的某个按钮被点击了,这个时候需要去主控制器中刷新数据。有一种做法是,让这个视图拥有控制器这个属性,然后当按钮被点击的时候去利用该属性去做刷新数据的操作。另一种做法是把控制器设置为这个视图的代理,当视图中的某个按钮被点击的时候,通知它的代理(主控制器)去干刷新数据这件事。
(2)要成为代理是由条件的,有以下几个步骤
1).双方约定一个协议(代理协议,注意命名规范),在视图中自定义一个协议,协议中提供一个方法。

复制代码 代码如下:

@protocol yyheaderviewdelegate <nsobject>

-(void)headerviewdidclickheaderview:(yyheaderview *)headerview;

@end


2).在视图中添加一个id类型的属性变量,任何人只要遵守了约定协议的都可以成为它的代理。
//delegate遵守yyheaderviewdelegate这个协议,可以使用协议中的方法
复制代码 代码如下:

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

3).在控制器中,遵守自定义的代理协议,就可以使用代理提供的方法,在这个方法中对数据进行刷新。
复制代码 代码如下:

@interface yyviewcontroller ()<yyheaderviewdelegate>

-(void)headerviewdidclickheaderview:(yyheaderview *)headerview

{

    [self.tableview reloaddata];

}


4).把控制器设置作为按钮点击事件的代理。
headerview.delegate=self;