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文件

复制代码
 1 //  2 // YYQQGroupModel.h
 3 // 02-QQ好友列表(基本数据的加载)
 4 //  5 // Created by apple on 14-5-31.
 6 // Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8  9 #import <Foundation/Foundation.h>
10 11 @interface YYQQGroupModel : NSObject
12 /**
13  * 名称属性
14 */ 15 @property(nonatomic,copy)NSString *name;
16 /**
17  * 是否在线
18 */ 19 @property(nonatomic,copy)NSString *online;
20 /**
21  * 好友列表
22 */ 23 @property(nonatomic,strong)NSArray *friends;
24 25 -(instancetype)initWithDict:(NSDictionary *)dict;
26 +(instancetype) qqGroupModelWithDict:(NSDictionary *)dict;
27 @end
复制代码

YYQQGroupModel.m文件

复制代码
 1 //  2 // YYQQGroupModel.m
 3 // 02-QQ好友列表(基本数据的加载)
 4 //  5 // Created by apple on 14-5-31.
 6 // Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8  9 #import "YYQQGroupModel.h" 10 #import "YYFriendsModel.h" 11 12 @implementation YYQQGroupModel
13 -(instancetype)initWithDict:(NSDictionary *)dict
14 {
15 if (self=[super init]) {
16 //将字典转换为模型 17  [self setValuesForKeysWithDictionary:dict];
18 19 //定义一个数组来保存转换后的模型 20 NSMutableArray *models=[NSMutableArray arrayWithCapacity:self.friends.count];
21 for (NSDictionary *dict in self.friends) {
22 YYFriendsModel *friends=[YYFriendsModel friendsWithDict:dict];
23  [models addObject:friends];
24  }
25 _friends=[models copy];
26  }
27 return self;
28 }
29 30 +(instancetype)qqGroupModelWithDict:(NSDictionary *)dict
31 {
32 return [[self alloc]initWithDict:dict];
33 }
34 @end
复制代码

YYFriendsModel.h文件

复制代码
 1 //  2 // YYFriendsModel.h
 3 // 02-QQ好友列表(基本数据的加载)
 4 //  5 // Created by apple on 14-5-31.
 6 // Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8  9 #import <Foundation/Foundation.h>
10 11 @interface YYFriendsModel : NSObject
12 /**
13  * 每个好友的名称
14 */ 15 @property(nonatomic,copy)NSString *name;
16 /**
17  *每个好友的头像
18 */ 19 @property(nonatomic,copy)NSString *icon;
20 /**
21  * 每个好友的个性签名
22 */ 23 @property(nonatomic,copy)NSString *intro;
24 /**
25  * 该好友是否是vip
26 */ 27 @property(nonatomic,assign,getter = isVip)BOOL vip;
28 29 -(instancetype)initWithDict:(NSDictionary *)dict;
30 +(instancetype)friendsWithDict:(NSDictionary *)dict;
31 @end
复制代码

YYFriendsModel.m文件

复制代码
 1 //  2 // YYFriendsModel.m
 3 // 02-QQ好友列表(基本数据的加载)
 4 //  5 // Created by apple on 14-5-31.
 6 // Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8  9 #import "YYFriendsModel.h" 10 11 @implementation YYFriendsModel
12 -(instancetype)initWithDict:(NSDictionary *)dict
13 {
14 if (self=[super init]) {
15  [self setValuesForKeysWithDictionary:dict];
16  }
17 return self;
18 }
19 20 +(instancetype)friendsWithDict:(NSDictionary *)dict
21 {
22 return [[self alloc]initWithDict:dict];
23 }
24 @end
复制代码

视图部分

YYfriendCell.h文件

复制代码
 1 //  2 // YYfriendCell.h
 3 // 02-QQ好友列表(基本数据的加载)
 4 //  5 // Created by apple on 14-5-31.
 6 // Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8  9 #import <UIKit/UIKit.h>
10 @class YYFriendsModel;
11 @interface YYfriendCell : UITableViewCell
12 13 @property(nonatomic,strong)YYFriendsModel *friends;
14 15 +(instancetype)cellWithTableview:(UITableView *)tableView;
16 @end
复制代码

YYfriendCell.m文件

复制代码
 1 //  2 // YYfriendCell.m
 3 // 02-QQ好友列表(基本数据的加载)
 4 //  5 // Created by apple on 14-5-31.
 6 // Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8  9 #import "YYfriendCell.h" 10 #import "YYFriendsModel.h" 11 //私有扩展 12 @interface YYfriendCell()
13 14 15 @end 16 @implementation YYfriendCell
17 18 +(YYfriendCell *)cellWithTableview:(UITableView *)tableView
19 {
20 static NSString *identifier=@"qq";
21 YYfriendCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
22 if (cell==nil) {
23 //这里使用系统自带的样式 24 cell=[[YYfriendCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
25 NSLog(@"创建一个cell");
26  }
27 return cell;
28 }
29 30 -(void)setFriends:(YYFriendsModel *)friends
31 {
32 _friends=friends;
33 //1.设置头像 34 self.imageView.image=[UIImage imageNamed:_friends.icon];
35 //2.设置昵称 36 self.textLabel.text=_friends.name;
37 //3.设置简介 38 self.detailTextLabel.text=_friends.intro;
39 //判断是否是会员 40 /**
41  * 这里有个注意点,如果不写else设置为黑色,会怎么样?
42 */ 43 if (_friends.isVip) {
44  [self.textLabel setTextColor:[UIColor redColor]];
45 }else 46  {
47  [self.textLabel setTextColor:[UIColor blackColor]];
48  }
49 }
50 @end
复制代码

主控制器部分

YYViewController.m文件

复制代码
 1 //  2 // YYViewController.m
 3 // 02-QQ好友列表(基本数据的加载)
 4 //  5 // Created by apple on 14-5-31.
 6 // Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8  9 #import "YYViewController.h" 10 #import "YYQQGroupModel.h" 11 #import "YYfriendCell.h" 12 #import "YYFriendsModel.h" 13 14 @interface YYViewController ()
15 /**
16  * 用来保存所有的分组数据
17 */ 18 @property(nonatomic,strong)NSArray *groupFriends;
19 @end 20 21 @implementation YYViewController
22 #pragma mark-懒加载
23 //1.先拿到数据,实现懒加载 24 -(NSArray *)groupFriends
25 {
26 if (_groupFriends==nil) {
27 NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"friends.plist" ofType:nil];
28 NSArray *arrayM=[NSArray arrayWithContentsOfFile:fullpath];
29 30 NSMutableArray *models=[NSMutableArray arrayWithCapacity:arrayM.count];
31 for (NSDictionary *dict in arrayM) {
32 YYQQGroupModel *group=[YYQQGroupModel qqGroupModelWithDict:dict];
33  [models addObject:group];
34  }
35 _groupFriends=[models copy];
36  }
37 return _groupFriends;
38 }
39 40 - (void)viewDidLoad
41 {
42  [super viewDidLoad];
43 self.tableView.sectionHeaderHeight = 100;
44 }
45 46 #pragma mark- 设置数据源
47 //返回多少组
48 //为什么这里不会智能提示?因为这些方法是uitableview协议里的,默认并没有遵守协议,让主控制器类继承uitableviewcontroller后,就已经遵守了 49 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
50 {
51 return self.groupFriends.count;
52 }
53 //每组返回多少行 54 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
55 {
56 //取出对应的组模型 57 YYQQGroupModel *group=self.groupFriends[section];
58 //返回对应组中的好友数 59 return group.friends.count;
60 }
61 //每组每行的内容 62 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
63 {
64 //1.创建cell 65 YYfriendCell *cell=[YYfriendCell cellWithTableview:tableView];
66 67 //2.设置cell 68 YYQQGroupModel *group=self.groupFriends[indexPath.section];
69 YYFriendsModel *friends=group.friends[indexPath.row];
70 cell.friends=friends;
71 //3.返回一个cell 72 return cell;
73 }
74 75 76 #pragma mark - 代理方法
77 // 当一个分组标题进入视野的时候就会调用该方法 78 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
79 {
80 // 1.创建头部视图 81 UIView *view = [[UIView alloc] init];
82 view.backgroundColor = [UIColor grayColor];
83 // 2.返回头部视图 84 return view;
85 }
86 87 //设置分组头部标题的高度 88 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
89 {
90 return 44;
91 }
92 93 #pragma mark 隐藏状态栏
94 -(BOOL)prefersStatusBarHidden
95 {
96 return YES;
97 }
98 @end
复制代码

实现的简陋效果:

三、注意点

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

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

目录
相关文章
|
20天前
|
前端开发 编解码 数据格式
浅谈响应式编程在企业级前端应用 UI 开发中的实践
浅谈响应式编程在企业级前端应用 UI 开发中的实践
20 0
浅谈响应式编程在企业级前端应用 UI 开发中的实践
|
1月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
84 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
6天前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
【4月更文挑战第18天】**Cocoa Touch** 是iOS和Mac OS X应用的核心框架,包含面向对象库、运行时系统和触摸优化工具。它提供Mac验证的开发模式,强调触控接口和性能,涵盖3D图形、音频、网络及设备访问API,如相机和GPS。是构建高效iOS应用的基础,对开发者至关重要。
9 0
|
16天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
21天前
|
开发工具 Swift iOS开发
利用SwiftUI构建动态用户界面:iOS开发新范式
【4月更文挑战第3天】 随着苹果不断推进其软件开发工具的边界,SwiftUI作为一种新兴的编程框架,已经逐渐成为iOS开发者的新宠。不同于传统的UIKit,SwiftUI通过声明式语法和强大的功能组合,为创建动态且响应式的用户界面提供了一种更加简洁高效的方式。本文将深入探讨如何利用SwiftUI技术构建具有高度自定义能力和响应性的用户界面,并展示其在现代iOS应用开发中的优势和潜力。
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
193 2
QGS
|
3月前
|
前端开发 数据可视化 Java
手拉手JavaFX UI控件与springboot3+FX桌面开发(下)
手拉手JavaFX UI控件与springboot3+FX桌面开发
QGS
66 0
QGS
|
3月前
|
前端开发
手拉手JavaFX UI控件与springboot3+FX桌面开发(中)
手拉手JavaFX UI控件与springboot3+FX桌面开发
QGS
89 0
QGS
|
3月前
|
API 数据安全/隐私保护 索引
手拉手JavaFX UI控件与springboot3+FX桌面开发(上)
手拉手JavaFX UI控件与springboot3+FX桌面开发
QGS
67 1