码迷,mamicode.com
首页 > 其他好文 > 详细

QQ好友列表

时间:2016-05-25 00:28:37      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

 一 好友的页面的搭建(tableView)

1. 控制器代码

#pragma mark - 数据源方法

// 返回有多少组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.groups.count;

}

 

// 返回每一组有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    HMFriendGroup *group = self.groups[section];

    

    // 如果是展开就返回真实的行数,如果没有展开就返回0行

    return group.isOpen ? group.friends.count : 0;

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // 1.创建cell

    HMFriendCell *cell = [HMFriendCell cellWithTableView:tableView];

 

    // 2.设置数据

    cell.friendData = [self.groups[indexPath.section] friends][indexPath.row];

    // 返回cell

    return cell;

}

2. 好友的cell的设置

#import "HMFriendCell.h"

#import "HMFriend.h"

 

@implementation HMFriendCell

 

+ (instancetype)cellWithTableView:(UITableView *)tableView {

    return [tableView dequeueReusableCellWithIdentifier:@"friend"];

}

 

 

// 重写模型属性的set方法给cell内部的子控件设置数据

- (void)setFriendData:(HMFriend *)friendData {

    _friendData = friendData;

    

 

    self.imageView.image = [UIImage imageNamed:friendData.icon];

    self.textLabel.text = friendData.name;

    self.detailTextLabel.text = friendData.intro;

    

//    if (friendData.isVip) { // 是vip把昵称设置为红色

//        self.textLabel.textColor = [UIColor redColor];

//    } else {

//        self.textLabel.textColor = [UIColor blackColor];

//    }

    

    self.textLabel.textColor = (friendData.isVip) ? [UIColor redColor] : [UIColor blackColor];

}

@end

二 头部 QQ分组的设置

1. 控制器中的设置

#import "ViewController.h"

#import "HMFriendGroup.h"

#import "HMFriend.h"

#import "HMFriendCell.h"

#import "HMGroupHeaderView.h"

 

@interface ViewController ()<HMGroupHeaderViewDelegate>

// 保存所有模型数据

@property (nonatomic, strong) NSArray *groups;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 设置每一组的头部高度

    self.tableView.sectionHeaderHeight = 44;

    

    // 只要给tableView设置一个尾部视图,多出来的分割线就会没了

    self.tableView.tableFooterView = [[UIView alloc] init];

    

    

    // 监听通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(groupHeaderViewDidOpenGroup:) name:@"openGroup" object:nil];

}

 

// 返回每一组的头部视图

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    NSLog(@"------%zd", section);

    // 1.创建headerView

    HMGroupHeaderView *headerView = [HMGroupHeaderView headerViewWithTableView:tableView];

 

    // 设置代理

    headerView.delegate = self;

    

    // 2.设置数据

    headerView.group = self.groups[section];

    

    // 设置tag

    headerView.tag = section;

    // 3.返回headerView

    return headerView;

}

 

 

#pragma mark - headerView的代理方法

- (void)groupHeaderViewDidOpenGroup:(NSNotification *)note {

    // 刷新表格,让数据源方法重新执行

//    [self.tableView reloadData];

//    NSLog(@"%zd", headerView.tag);

    // 获取通知的发布者

   HMGroupHeaderView *headerView = note.object;

    // 创建表示组的集合索引

    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:headerView.tag];

    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];

}

 

- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

// 隐藏状态栏

- (BOOL)prefersStatusBarHidden {

    return YES;

}

#pragma mark - 懒加载

- (NSArray *)groups {

    if (_groups == nil) {

        // 读取plist

        NSArray *dictArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil]];

        

        // 用来存放所有组模型的可变数组

        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:dictArr.count];

        

        // 遍历字典数组转换组模型

        for (NSDictionary *dict in dictArr) {

            // 组的字典转换组的模型

            HMFriendGroup *group = [HMFriendGroup groupWithDict:dict];

            [arrM addObject:group];

        }

        _groups = arrM;

    }

    return _groups;

}

@end

 

2. QQ还有分组的cell的设置

#import "HMGroupHeaderView.h"

#import "HMFriendGroup.h"

 

@interface HMGroupHeaderView ()

// 显示组名的按钮

@property (nonatomic, weak) UIButton *headerBtn;

// 在线人数

@property (nonatomic, weak) UILabel *onlineLabel;

 

@end

@implementation HMGroupHeaderView

 

+ (instancetype)headerViewWithTableView:(UITableView *)tableView {

    // 1.定义重用标识

    static NSString *ID = @"group";

    // 2.先去缓存池中找可重用的headerView

    HMGroupHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];

    // 3.如果缓存池中没有可重用的headerView,创建一个新的,并设置重用标识

    if (headerView == nil) {

        headerView = [[HMGroupHeaderView alloc] initWithReuseIdentifier:ID];

    }

    return headerView;

}

 

 

// 重写初始化方法,在此方法内部创建和添加所有用来显示数据的子控件

// 一定不要在init方法去设置frame

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithReuseIdentifier:reuseIdentifier];

    if (self) {

        // 1.显示组名的按钮

        UIButton *headerBtn = [[UIButton alloc] init];

        // 设置按钮的背景图片

        [headerBtn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];

        [headerBtn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];

        // 设置按钮中的文字颜色

        [headerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        // 设置按钮的小图片

        [headerBtn setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];

        // 设置按钮中的内容水平方向左对齐

        headerBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

        

        

        // 设置按钮的内容内边距

        headerBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

        

        // 设置按钮中的图片的内边距

//        headerBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);

        // 设置按钮中的文字的内边距

        

        headerBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

        

        // 设置按钮中的小图片的填充模式

        headerBtn.imageView.contentMode = UIViewContentModeCenter;

        // 设置imageView中的图片超出边界之后不裁剪

//        headerBtn.imageView.clipsToBounds = NO;

        headerBtn.imageView.layer.masksToBounds = NO;

        

        

        [self.contentView addSubview:headerBtn];

        self.headerBtn = headerBtn;

        

        // 给按钮添加点击方法

        [headerBtn addTarget:self action:@selector(headerBtnClick) forControlEvents:UIControlEventTouchUpInside];

        

        

        

        // 2.在线人数的label

        UILabel *onlineLabel = [[UILabel alloc] init];

        [self.contentView addSubview:onlineLabel];

        self.onlineLabel = onlineLabel;

//        onlineLabel.backgroundColor = [UIColor redColor];

        // 设置文字右对齐

        onlineLabel.textAlignment = NSTextAlignmentRight;

        

 

        

            }

    return self;

}

 

 

// 头部按钮的点击方法

- (void)headerBtnClick {

    // 对上一次的结果取反

    self.group.open = !self.group.isOpen;

    

    

    // 发布通知

    [[NSNotificationCenter defaultCenter] postNotificationName:@"openGroup" object:self userInfo:nil];

}

 

// 布局子控件"调整子控件的大小和位置"

 

- (void)layoutSubviews {

#warning mark - 重写此方法一定要super

    [super layoutSubviews];

 

    // 设置按钮的frame

    self.headerBtn.frame = self.bounds;

    

    // 在线人数label

    CGFloat onlineY = 0;

    CGFloat onlineW = 100;

    CGFloat onlineH = self.bounds.size.height;

    CGFloat onlineX = self.bounds.size.width - 8 - onlineW;

    self.onlineLabel.frame = CGRectMake(onlineX, onlineY, onlineW, onlineH);

 

}

 

// 重写组模型属性的set方法在此方法给子控件设置数据

- (void)setGroup:(HMFriendGroup *)group {

    _group = group;

    

    [self.headerBtn setTitle:group.name forState:UIControlStateNormal];

    

    self.onlineLabel.text = [NSString stringWithFormat:@"%@/%zd", group.online, group.friends.count];

    

    // 对按钮是否旋转的状态做判断

    if (group.isOpen) {

        self.headerBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);

    } else { // 如果当前这一组没有展示就应该让它回到初始值

        self.headerBtn.imageView.transform = CGAffineTransformIdentity;

 

    }

}

@end

 三 注意:

当一个控件不显示的时候有那些原因

 1.没有添加到父控件上

 2.没有设置frame

 3.frame设置的不对

 4.被其它控件遮盖住了

 5.透明度 >= 0.01

 6.hiden = YES

 7.子控件和父控件的背景色一样

 8.父控件的以上所有原因

 

QQ好友列表

标签:

原文地址:http://www.cnblogs.com/wangdjblogs/p/5525358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!