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

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

二、实现代码

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中进行了关联

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

实现的简陋效果:

三、注意点

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

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

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

二、实现代码

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.项目文件结构

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;

(0)

相关推荐

  • IOS实现展开二级列表效果

    先来看看效果图 用法(类似UITableView) 初始化XDMultTableView #import "XDMultTableView.h" ... @property(nonatomic, readwrite, strong)XDMultTableView *tableView; _tableView = [[XDMultTableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, sel

  • Android实现三级联动下拉框 下拉列表spinner的实例代码

    主要实现办法:动态加载各级下拉值的适配器 在监听本级下拉框,当本级下拉框的选中值改变时,随之修改下级的适配器的绑定值              XML布局: 复制代码 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_w

  • iOS实现列表与网格两种视图的相互切换

    下图为京东商城的截图 很多人看到这个,第一眼想到的是用TableView和CollectionView来做切换,笔者刚开始也是认为这么做,后来发现还有一个非常的简单方法,就可以实现这个功能. 实现代码 1.首先创建一个CollectionView. - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlo

  • iOS多级列表实现代码

    在项目开发中,层级列表经常遇到,简单点的二级列表利用UITableView的Header就可以实现,再简单点的三级列表通过对Cell高度进行调整也可以实现三级列表的效果.但遇到多级列表,尤其是层次不明的动态列表就比较麻烦了. 原理 层级列表和树形结构比较类似,不过不是二叉树,而是多叉树.每个节点只需要拥有指向父节点和子节点的两个指针,就能形成一颗树.我们将多级列表中每一级对象看作一个node,node拥有两个属性,分别为父节点和子节点的ID. 每棵树有个一个虚拟的root节点,它的ID为root

  • IOS展开三级列表效果示例

    效果图如下: #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end #import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate

  • IOS实现简易版的QQ下拉列表

    下面我们通过实例代码来一步步看怎么实现, 首先建立了两个模型类, 一个Friend, 一个FriendGroup类. 数据源用的本地的一个plist文件. plist文件中包含了FriendGroup的name,friends数组等属性. Friend.h 示例代码 #import <Foundation/Foundation.h> @interface Friend : NSObject @property (nonatomic, copy) NSString *name; @end Fri

  • iOS功能实现之列表的横向刷新加载

    库命名为PSRefresh,支持UIScrollView及所有UIScrollView的子类控件,UITableView(横向的tableVIew)及UICollectionView等皆可. 支持自定义文字,支持自定义gif图,可设置是否为最后一页. 本文一共提供了三种样式,分别是普通样式.gif加载样式(带有状态label).git加载样式(不带有状态label). Demo展示如下: 使用时导入 "UIScrollView+PSRefresh.h" 文件即可,文件中提供的属性及接口

  • ios基于UITableViewController实现列表

    实现效果图如下: News.h #import <Foundation/Foundation.h> @interface News : NSObject @property (nonatomic, strong) NSString *title; @property (nonatomic) NSUInteger count; @property (nonatomic, strong) NSString *imageName; + (NSArray *)demoData; @end<str

  • 讲解iOS开发中UITableView列表设计的基本要点

    一.UITableView简单介绍 1.tableView是一个用户可以滚动的多行单列列表,在表视图中,每一行都是一个UITableViewCell对象,表视图有两种风格可选 复制代码 代码如下: typedef NS_ENUM(NSInteger, UITableViewStyle) {     UITableViewStylePlain,                  // regular table view     UITableViewStyleGrouped           

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

    基本实现 一.项目结构和plist文件 二.实现代码 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 YY

  • iOS开发中Swift 指纹验证功能模块实例代码

    iOS调用TouchID代码: override func viewDidLoad() { super.viewDidLoad() let context = LAContext() var error: NSError? = nil let canEvaluatePolicy = context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) as Bool if error

  • iOS逆向开发之微信自动添加好友功能

    这一次,小程演示怎么让一个APP自动地运行,从而代替手工的操作.同样以"微信"以例,实现在一个微信群里面,对所有的成员,自动地一个一个地发出添加好友的请求. 知识点还是之前介绍的东西,流程方面还是跟踪与最终注入.因为这是一个系列的文章讲解(微信公众号"广州小程" -> 逆向开发),所以读者可以联系前面的文章来理解,用自己的话"翻译"成自己的知识与经验. 本文解决一个问题:如何让第三方程序自动化地运行. (一)批量添加好友的效果 小程使用&q

  • Android开发中应用程序分享功能实例

    本文实例讲述了Android开发中应用程序分享功能.分享给大家供大家参考,具体如下: Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); //设置类型 shareIntent.setType("text/plain"); //设置分享的主题 shareIntent.putExtra("android.intent.extra.SUBJECT", "分享&

  • Android开发中模仿qq列表信息滑动删除功能

    这个效果的完成主要分为两个部分 自定义view作为listview的列表项 一个view里面包括 显示头像,名字,消息内容等的contentView和滑动才能显示出来的删除,置顶的右边菜单menuView 在手指移动的时候同时改变这两个视图的位置 重写listview 判断item向左还是向右滑动 正常的滚动还是左右滑动等等 重写onTouchEvent 进行事件分发 大致思路: listview进行事件分发,判断需要滑动还是滚动等状态,如果需要滑动将事件传递给item进行滑动处理. 在item

  • java微信开发中的地图定位功能

    页面代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+&q

  • iOS程序开发中设置UITableView的全屏分隔线的方法(不画线)

    ableView是app开发中常用到的控件,功能很强大,多用于数据的显示.下面给大家介绍设置UITableView的全屏分隔线的两种方法. 具体详情如下所示: 如图 添加如下代码 sTableView.separatorInset = UIEdgeInsetsZero; sTableView.layoutMargins = UIEdgeInsetsZero; cell.layoutMargins = UIEdgeInsetsZero; 第二种方法如下图 -(void)viewDidLayoutS

  • iOS App开发中使cell高度自适应的黑魔法详解

    在使用 table view 的时侯经常会遇到这样的需求:table view 的 cell 中的内容是动态的,导致在开发的时候不知道一个 cell 的高度具体是多少,所以需要提供一个计算 cell 高度的算法,在每次加载到这个 cell 的时候计算出 cell 真正的高度. 在 iOS 8 之前 没有使用 Autolayout 的情况下,需要实现 table view delegate 的 tableView(tableView: UITableView, heightForRowAtInde

  • iOS App开发中扩展RCLabel组件进行基于HTML的文本布局

    iOS系统是一个十分注重用户体验的系统,在iOS系统中,用户交互的方案也十分多,然而要在label中的某部分字体中添加交互行为确实不容易的,如果使用其他类似Button的控件来模拟,文字的排版又将是一个解决十分困难的问题.这个问题的由来是项目中的一个界面中有一些广告位标签,而这些广告位的标签却是嵌在文本中的,当用户点击文字标签的位置时,会跳转到响应的广告页. CoreText框架和一些第三方库可以解决这个问题,但直接使用CoreText十分复杂,第三方库多注重于富文本的排版,对类似文字超链接的支

  • iOS应用开发中SQLite的初步配置指南

    iOS开发数据库篇-SQLite简单介绍 一.离线缓存 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等. 说明:离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式 (1)归档:NSCodeing.NSKeyedArchiver (2)偏好设置:NSUserDefaults (3)Plist存储:writeToFile 提示:上述三种方法都有一个致命的缺点,那就是都无法存储大批量的数据,有性能的问题. 举例:使用归档 两个问题: (1)数据的存取都必须是完整的,要求

随机推荐