iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
一、项目结构和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 = ;
} #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 ;
} #pragma mark 隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
return YES;
}
@end
实现的简陋效果:

三、注意点
(1)设置头部视图的方法

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

iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)的更多相关文章
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
一.实现效果 二.实现代码 1.数据模型部分 YYQQGroupModel.h文件 // // YYQQGroupModel.h // 02-QQ好友列表(基本数据的加载) / ...
- OS开发UI篇—使用UItableview完成一个简单的QQ好友列表
本文转自:http://www.cnblogs.com/wendingding/p/3763330.html 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableVi ...
- iOS开发UI篇—实现UItableview控件数据刷新
iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...
- iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...
- iOS开发UI篇—在UItableview中实现加载更多功能
一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据. 二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时候,主页面(主控制器 ...
- iOS开发UI篇—CALayer简介
iOS开发UI篇—CALayer简介 一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实 ...
- iOS开发UI篇—Button基础
iOS开发UI篇—Button基础 一.简单说明 一般情况下,点击某个控件后,会做出相应反应的都是按钮 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置 二.按钮的三种状 ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
- iOS开发UI篇—UITableview控件简单介绍
iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...
随机推荐
- python核心编程学习记录之条件和循环
- Apple开发者账号简介
苹果对开发者主要分为3类:个人.组织(公司.企业).教育机构.即: 1.个人(Individual) 2.组织(Organizations) 组织类又分为2个小类: (1)公司(Company) (2 ...
- 导出db2数据库的表结构和数据(转载)
对于db2数据库,导入和导出表结构和数据其实很简单,只需要用到db2look和db2move两个命令即可.这两个命令都需要在客户端的命令行处理器 中执行,但对于数据库服务器和客户端不在同一机器上的 ...
- POJ 3436:ACM Computer Factory(最大流记录路径)
http://poj.org/problem?id=3436 题意:题意很难懂.给出P N.接下来N行代表N个机器,每一行有2*P+1个数字 第一个数代表容量,第2~P+1个数代表输入,第P+2到2* ...
- inotify+rsync目录实时同步
两台linux服务器系统CentOS7 一台Apache IP:192.168.155.130(发布文件服务器,也可以叫rsync客户端) 一台nginx IP:192.168.155.131(同步镜 ...
- [转]使用xcode4 workspace 多个project协同工作
转载地址:http://erlangprogram.com/use-xcode4-workspace 一般的某个应用单独新建一个 project 就可以了,然后把所有的程序文件都放在里面,这个可以 ...
- Tomcat 发布war包提示war包超出大小修改
error信息: java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$Size ...
- listivew 动态刷新单个item
使用ViewHolder来刷新某项数据,而不用每次都全部刷新数据. 继承BaseAdapter,新建ViewHolder类. public class TestListAdapter extends ...
- mybatis代码生成(generator工具生成代码)
generator工具生成代码 下载地址 http://pan.baidu.com/s/1bY8C0I
- lievent源码分析:evbuffer
struct evbuffer定义在evbuffer-internal.h文件中. evbuffer结构内部保存一个以evbuffer-chain结构为节点的链表,evbuffer内部有两个分别指向首 ...