QQList
2015-07-24 16:26
351 查看
思路:采用 MVC的思想来进行编写该界面
首先是模型:
//
// FUTHFriend.h
// QQ好友列表
//
// Created by future on 15/7/23.
// Copyright (c) 2015年 future. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface FUTHFriend : NSObject
@property (nonatomic,copy)NSString* name;
@property (nonatomic,copy)NSString* icon;
@property (nonatomic,copy)NSString* intro;
@property (nonatomic,assign,getter=isVip)BOOL vip;
+(instancetype)friendWithDict:(NSDictionary*)dict;
-(instancetype)initWithDict:(NSDictionary*)dict;
@end
//
// FUTHFriend.m
// QQ好友列表
//
// Created by future on 15/7/23.
// Copyright (c) 2015年 future. All rights reserved.
//
#import "FUTHFriend.h"
@implementation FUTHFriend
+(instancetype)friendWithDict:(NSDictionary*)dict
{
FUTHFriend* f = [[FUTHFriendalloc]initWithDict:dict];
return f;
}
-(instancetype)initWithDict:(NSDictionary*)dict{
if (self = [superinit]) {
[selfsetValuesForKeysWithDictionary:dict];
}
return self;
}
@end
#import <Foundation/Foundation.h>
@interface FUTHFriendGroup : NSObject
@property (nonatomic,copy)NSString* name;
@property (nonatomic,strong)NSArray* friends;
@property (nonatomic,assign)int online;
/**
*
是否展开
*/
@property (nonatomic,assign,getter=isOpen)BOOL open;
+(instancetype)groupWithDict:(NSDictionary*)dict;
-(instancetype)initWithDict:(NSDictionary*)dict;
@end
#import "FUTHFriendGroup.h"
#import "FUTHFriend.h"
@implementation FUTHFriendGroup
+(instancetype)groupWithDict:(NSDictionary*)dict{
FUTHFriendGroup* f = [[FUTHFriendGroupalloc]initWithDict:dict];
return f;
}
-(instancetype)initWithDict:(NSDictionary*)dict{
if (self = [superinit]) {
//注入所以属性
[selfsetValuesForKeysWithDictionary:dict];
//特殊处理 friends
NSMutableArray* mtArray = [NSMutableArrayarray];
for (NSDictionary* dictin self.friends) {
FUTHFriend* friend = [[FUTHFriendalloc] initWithDict:dict];
[mtArray addObject:friend];
}
_friends = mtArray;
}
returnself;
}
@end
然后视图:
#import <UIKit/UIKit.h>
@classFUTHFriend;
@interface FUTHFriendCell : UITableViewCell
+ (instancetype)cellWithTableView:(UITableView*)tableView;
@property (nonatomic,strong)FUTHFriend* friendData;
@end
#import "FUTHFriendCell.h"
#import "FUTHFriend.h"
@implementation FUTHFriendCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
+ (instancetype)cellWithTableView:(UITableView*)tableView{
static NSString* ID = @"friends";
FUTHFriendCell* cell = [tableViewdequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[FUTHFriendCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:ID];
}
return cell;
}
-(void)setFriendData:(FUTHFriend *)friendData{
self.imageView.image = [UIImageimageNamed:friendData.icon];
self.textLabel.text = friendData.name;
self.detailTextLabel.text = friendData.intro;
}
@end
#import <UIKit/UIKit.h>
@classFUTHFriendGroup;
@protocol FUTHFriendGroupDelegate <NSObject>
-(void)headerViewDidClickedNameView;
@end
@interface FUTHHeaderView :UITableViewHeaderFooterView
@property (nonatomic,strong)FUTHFriendGroup* group;
@property (nonatomic,weak)id<FUTHFriendGroupDelegate> delegate;
+(instancetype)headerWithTableView:(UITableView*)tableView;
@end
#import "FUTHFriendGroup.h"
#import "FUTHHeaderView.h"
@interfaceFUTHHeaderView()
@property (nonatomic,weak)UILabel* countView;
@property (nonatomic,weak)UIButton* nameView;
@end
@implementation FUTHHeaderView
/*
某个控件出不来
1.frame
的尺寸
2hidden
是否为 yes
3.有没有添加到父控件
4.alpha
是否为<0.01
5.被其他控件挡住了
6.父控件的前面五个情况
*/
+(instancetype)headerWithTableView:(UITableView*)tableView{
static NSString* ID = @"header";
FUTHHeaderView* header = [tableViewdequeueReusableHeaderFooterViewWithIdentifier:ID];
if (header == nil ) {
header = [[FUTHHeaderViewalloc] initWithReuseIdentifier:ID];
}
return header;
}
//在初始化方法中FUTHHeaderView
的 frame\bounds
为0
-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
if (self = [superinitWithReuseIdentifier:reuseIdentifier]) {
//add btn
UIButton* nameView = [UIButtonbuttonWithType:UIButtonTypeCustom];
//
// nameView.frame = self.bounds;
nameView.backgroundColor = [UIColorredColor];
// [nameView setBackgroundColor:[UIColor redColor] forState:UIControlStateNormal];
[nameView setImage:[UIImageimageNamed:@"buddy_header_arrow"]forState:UIControlStateNormal];
[nameView setBackgroundImage:[UIImageimageNamed:@"buddy_header_bg"]forState:UIControlStateNormal];
[nameView setBackgroundImage:[UIImageimageNamed:@"buddy_header_bg_highlighted"]forState:UIControlStateHighlighted];
nameView.titleLabel.textAlignment =NSTextAlignmentLeft;
[nameView setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
//对齐方式
nameView.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
//边距
nameView.contentEdgeInsets =UIEdgeInsetsMake(0,10,0,0);
nameView.titleEdgeInsets =UIEdgeInsetsMake(0,10,0,0);
_nameView = nameView;
//add click event
[nameView addTarget:selfaction:@selector(nameViewClicked)forControlEvents:UIControlEventTouchUpInside];
//设置按钮内部的 imageView的内容模式为居中
nameView.imageView.contentMode =UIViewContentModeCenter;
//不裁剪图片
nameView.imageView.clipsToBounds =NO;
[self.contentViewaddSubview:nameView];
//add friend count
UILabel* countlabel = [[UILabelalloc] init];
countlabel.textColor = [UIColorblackColor];
[self.contentViewaddSubview:countlabel];
countlabel.textAlignment =NSTextAlignmentRight;
_countView = countlabel;
}
returnself;
}
/**
* 头部的点击事件
*
* @return void
*/
-(void)nameViewClicked{
_group.open = !_group.open;
if ([_delegaterespondsToSelector:@selector(headerViewDidClickedNameView)]) {
[_delegateheaderViewDidClickedNameView];
}
//在此调用不行,因为 reloadData会刷新控件
// if (_group.isOpen) {
// self.nameView.imageView.transform = CGAffineTransformMaker
// }
}
/**
* 当控件被加入的时候会自动调用
*/
-(void)didMoveToSuperview{
if (_group.isOpen) {
self.nameView.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
} else{
self.nameView.imageView.transform = CGAffineTransformMakeRotation(0);
}
}
/**
* 当一个控件的 frame发生改变的时候就会调用
一般在这里布局内部的子控件的 frame
*
* @return void
*/
-(void)layoutSubviews{
#warning
一定要调用 super
的方法
[superlayoutSubviews];
//set nameView
_nameView.frame =self.bounds;
//set
好友的 frame
CGFloat countY = 0;
CGFloat countH =self.bounds.size.height;
CGFloat countW = 150;;
CGFloat countX = self.bounds.size.width -10 - countW;
_countView.frame =CGRectMake(countX , countY, countW, countH);
}
-(void)setGroup:(FUTHFriendGroup *)group{
//set nameView
_group = group;
[_nameViewsetTitle:group.nameforState:UIControlStateNormal];
//set countView
_countView.text = [NSStringstringWithFormat:@"%d/%d",_group.online,_group.friends.count];
}
@end
最后控制器:
#import <UIKit/UIKit.h>
@interface ViewController :UITableViewController
@end
#import "ViewController.h"
#import "FUTHFriend.h"
#import "FUTHFriendGroup.h"
#import "FUTHHeaderView.h"
#import "FUTHFriendCell.h"
@interfaceViewController ()<FUTHFriendGroupDelegate>
@property (nonatomic,strong)NSArray* groups;
@end
@implementation ViewController
#pragma mark -FriendGroup
-(NSArray*)groups{
if (_groups ==nil) {
NSArray* dictArray = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"friends.plist"ofType:nil]];
NSMutableArray* groupArray = [NSMutableArrayarray];
for (NSDictionary*dictin dictArray) {
FUTHFriendGroup* group = [[FUTHFriendGroupalloc]initWithDict:dict];
[groupArray addObject:group];
}
_groups = groupArray;
}
return_groups;
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView.sectionHeaderHeight =40;
}
-(BOOL)prefersStatusBarHidden{
returnYES;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -TableDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
returnself.groups.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
BOOL open = [self.groups[section]isOpen];
if (open) {
return [self.groups[section]friends].count;
} else{
return 0;
}
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//set cell
FUTHFriendGroup* group = self.groups[indexPath.section];
FUTHFriend* friend = group.friends[indexPath.row];
FUTHFriendCell* cell = [FUTHFriendCellcellWithTableView:tableView];
//set data
cell.friendData = friend;
return cell;
}
//-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// return [self.groups[section] name];
//}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
FUTHHeaderView* header = [FUTHHeaderViewheaderWithTableView:tableView];
header.group = _groups[section];
header.delegate = self;
return header;
}
-(void)headerViewDidClickedNameView{
[self.tableViewreloadData];
}
@end
首先是模型:
//
// FUTHFriend.h
// QQ好友列表
//
// Created by future on 15/7/23.
// Copyright (c) 2015年 future. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface FUTHFriend : NSObject
@property (nonatomic,copy)NSString* name;
@property (nonatomic,copy)NSString* icon;
@property (nonatomic,copy)NSString* intro;
@property (nonatomic,assign,getter=isVip)BOOL vip;
+(instancetype)friendWithDict:(NSDictionary*)dict;
-(instancetype)initWithDict:(NSDictionary*)dict;
@end
//
// FUTHFriend.m
// QQ好友列表
//
// Created by future on 15/7/23.
// Copyright (c) 2015年 future. All rights reserved.
//
#import "FUTHFriend.h"
@implementation FUTHFriend
+(instancetype)friendWithDict:(NSDictionary*)dict
{
FUTHFriend* f = [[FUTHFriendalloc]initWithDict:dict];
return f;
}
-(instancetype)initWithDict:(NSDictionary*)dict{
if (self = [superinit]) {
[selfsetValuesForKeysWithDictionary:dict];
}
return self;
}
@end
#import <Foundation/Foundation.h>
@interface FUTHFriendGroup : NSObject
@property (nonatomic,copy)NSString* name;
@property (nonatomic,strong)NSArray* friends;
@property (nonatomic,assign)int online;
/**
*
是否展开
*/
@property (nonatomic,assign,getter=isOpen)BOOL open;
+(instancetype)groupWithDict:(NSDictionary*)dict;
-(instancetype)initWithDict:(NSDictionary*)dict;
@end
#import "FUTHFriendGroup.h"
#import "FUTHFriend.h"
@implementation FUTHFriendGroup
+(instancetype)groupWithDict:(NSDictionary*)dict{
FUTHFriendGroup* f = [[FUTHFriendGroupalloc]initWithDict:dict];
return f;
}
-(instancetype)initWithDict:(NSDictionary*)dict{
if (self = [superinit]) {
//注入所以属性
[selfsetValuesForKeysWithDictionary:dict];
//特殊处理 friends
NSMutableArray* mtArray = [NSMutableArrayarray];
for (NSDictionary* dictin self.friends) {
FUTHFriend* friend = [[FUTHFriendalloc] initWithDict:dict];
[mtArray addObject:friend];
}
_friends = mtArray;
}
returnself;
}
@end
然后视图:
#import <UIKit/UIKit.h>
@classFUTHFriend;
@interface FUTHFriendCell : UITableViewCell
+ (instancetype)cellWithTableView:(UITableView*)tableView;
@property (nonatomic,strong)FUTHFriend* friendData;
@end
#import "FUTHFriendCell.h"
#import "FUTHFriend.h"
@implementation FUTHFriendCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
+ (instancetype)cellWithTableView:(UITableView*)tableView{
static NSString* ID = @"friends";
FUTHFriendCell* cell = [tableViewdequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[FUTHFriendCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:ID];
}
return cell;
}
-(void)setFriendData:(FUTHFriend *)friendData{
self.imageView.image = [UIImageimageNamed:friendData.icon];
self.textLabel.text = friendData.name;
self.detailTextLabel.text = friendData.intro;
}
@end
#import <UIKit/UIKit.h>
@classFUTHFriendGroup;
@protocol FUTHFriendGroupDelegate <NSObject>
-(void)headerViewDidClickedNameView;
@end
@interface FUTHHeaderView :UITableViewHeaderFooterView
@property (nonatomic,strong)FUTHFriendGroup* group;
@property (nonatomic,weak)id<FUTHFriendGroupDelegate> delegate;
+(instancetype)headerWithTableView:(UITableView*)tableView;
@end
#import "FUTHFriendGroup.h"
#import "FUTHHeaderView.h"
@interfaceFUTHHeaderView()
@property (nonatomic,weak)UILabel* countView;
@property (nonatomic,weak)UIButton* nameView;
@end
@implementation FUTHHeaderView
/*
某个控件出不来
1.frame
的尺寸
2hidden
是否为 yes
3.有没有添加到父控件
4.alpha
是否为<0.01
5.被其他控件挡住了
6.父控件的前面五个情况
*/
+(instancetype)headerWithTableView:(UITableView*)tableView{
static NSString* ID = @"header";
FUTHHeaderView* header = [tableViewdequeueReusableHeaderFooterViewWithIdentifier:ID];
if (header == nil ) {
header = [[FUTHHeaderViewalloc] initWithReuseIdentifier:ID];
}
return header;
}
//在初始化方法中FUTHHeaderView
的 frame\bounds
为0
-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
if (self = [superinitWithReuseIdentifier:reuseIdentifier]) {
//add btn
UIButton* nameView = [UIButtonbuttonWithType:UIButtonTypeCustom];
//
// nameView.frame = self.bounds;
nameView.backgroundColor = [UIColorredColor];
// [nameView setBackgroundColor:[UIColor redColor] forState:UIControlStateNormal];
[nameView setImage:[UIImageimageNamed:@"buddy_header_arrow"]forState:UIControlStateNormal];
[nameView setBackgroundImage:[UIImageimageNamed:@"buddy_header_bg"]forState:UIControlStateNormal];
[nameView setBackgroundImage:[UIImageimageNamed:@"buddy_header_bg_highlighted"]forState:UIControlStateHighlighted];
nameView.titleLabel.textAlignment =NSTextAlignmentLeft;
[nameView setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
//对齐方式
nameView.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
//边距
nameView.contentEdgeInsets =UIEdgeInsetsMake(0,10,0,0);
nameView.titleEdgeInsets =UIEdgeInsetsMake(0,10,0,0);
_nameView = nameView;
//add click event
[nameView addTarget:selfaction:@selector(nameViewClicked)forControlEvents:UIControlEventTouchUpInside];
//设置按钮内部的 imageView的内容模式为居中
nameView.imageView.contentMode =UIViewContentModeCenter;
//不裁剪图片
nameView.imageView.clipsToBounds =NO;
[self.contentViewaddSubview:nameView];
//add friend count
UILabel* countlabel = [[UILabelalloc] init];
countlabel.textColor = [UIColorblackColor];
[self.contentViewaddSubview:countlabel];
countlabel.textAlignment =NSTextAlignmentRight;
_countView = countlabel;
}
returnself;
}
/**
* 头部的点击事件
*
* @return void
*/
-(void)nameViewClicked{
_group.open = !_group.open;
if ([_delegaterespondsToSelector:@selector(headerViewDidClickedNameView)]) {
[_delegateheaderViewDidClickedNameView];
}
//在此调用不行,因为 reloadData会刷新控件
// if (_group.isOpen) {
// self.nameView.imageView.transform = CGAffineTransformMaker
// }
}
/**
* 当控件被加入的时候会自动调用
*/
-(void)didMoveToSuperview{
if (_group.isOpen) {
self.nameView.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
} else{
self.nameView.imageView.transform = CGAffineTransformMakeRotation(0);
}
}
/**
* 当一个控件的 frame发生改变的时候就会调用
一般在这里布局内部的子控件的 frame
*
* @return void
*/
-(void)layoutSubviews{
#warning
一定要调用 super
的方法
[superlayoutSubviews];
//set nameView
_nameView.frame =self.bounds;
//set
好友的 frame
CGFloat countY = 0;
CGFloat countH =self.bounds.size.height;
CGFloat countW = 150;;
CGFloat countX = self.bounds.size.width -10 - countW;
_countView.frame =CGRectMake(countX , countY, countW, countH);
}
-(void)setGroup:(FUTHFriendGroup *)group{
//set nameView
_group = group;
[_nameViewsetTitle:group.nameforState:UIControlStateNormal];
//set countView
_countView.text = [NSStringstringWithFormat:@"%d/%d",_group.online,_group.friends.count];
}
@end
最后控制器:
#import <UIKit/UIKit.h>
@interface ViewController :UITableViewController
@end
#import "ViewController.h"
#import "FUTHFriend.h"
#import "FUTHFriendGroup.h"
#import "FUTHHeaderView.h"
#import "FUTHFriendCell.h"
@interfaceViewController ()<FUTHFriendGroupDelegate>
@property (nonatomic,strong)NSArray* groups;
@end
@implementation ViewController
#pragma mark -FriendGroup
-(NSArray*)groups{
if (_groups ==nil) {
NSArray* dictArray = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"friends.plist"ofType:nil]];
NSMutableArray* groupArray = [NSMutableArrayarray];
for (NSDictionary*dictin dictArray) {
FUTHFriendGroup* group = [[FUTHFriendGroupalloc]initWithDict:dict];
[groupArray addObject:group];
}
_groups = groupArray;
}
return_groups;
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView.sectionHeaderHeight =40;
}
-(BOOL)prefersStatusBarHidden{
returnYES;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -TableDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
returnself.groups.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
BOOL open = [self.groups[section]isOpen];
if (open) {
return [self.groups[section]friends].count;
} else{
return 0;
}
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//set cell
FUTHFriendGroup* group = self.groups[indexPath.section];
FUTHFriend* friend = group.friends[indexPath.row];
FUTHFriendCell* cell = [FUTHFriendCellcellWithTableView:tableView];
//set data
cell.friendData = friend;
return cell;
}
//-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// return [self.groups[section] name];
//}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
FUTHHeaderView* header = [FUTHHeaderViewheaderWithTableView:tableView];
header.group = _groups[section];
header.delegate = self;
return header;
}
-(void)headerViewDidClickedNameView{
[self.tableViewreloadData];
}
@end
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 分享下手机软件界面设计浅析
- winform异型不规则界面设计的实现方法
- Zend的MVC机制使用分析(二)
- ASP.NET MVC 4 捆绑和缩小实例介绍
- ASP.NET MVC中将控制器分离到类库的实现
- asp.net实现在非MVC中使用Razor模板引擎的方法
- ASP.NET MVC中的AJAX应用
- 如何在MVC应用程序中使用Jquery
- ASP.NET MVC小结之基础篇(二)
- ASP.NET小结之MVC, MVP, MVVM比较以及区别(一)
- Asp.net实现MVC处理文件的上传下载功能实例教程
- ASP.NET MVC小结之基础篇(一)
- Flex中最好的MVC框架Mate框架
- ASP.NET MVC学习笔记
- 关于php mvc开发模式的感想
- Android中设置只有程序第一次运行才显示的界面实现思路
- php打造属于自己的MVC框架
- 使用ASP.NET MVC 4 Async Action+jQuery实现消息通知机制的实现代码
- mvc下,3种窗口弹出设置的方法