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方法时,应该考虑到回滚。

目录
相关文章
|
1月前
|
iOS开发 开发者
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
143 67
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
|
4天前
|
JavaScript 搜索推荐 Android开发
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
23 8
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
|
2月前
|
iOS开发 开发者 MacOS
深入探索iOS开发中的SwiftUI框架
【10月更文挑战第21天】 本文将带领读者深入了解Apple最新推出的SwiftUI框架,这一革命性的用户界面构建工具为iOS开发者提供了一种声明式、高效且直观的方式来创建复杂的用户界面。通过分析SwiftUI的核心概念、主要特性以及在实际项目中的应用示例,我们将展示如何利用SwiftUI简化UI代码,提高开发效率,并保持应用程序的高性能和响应性。无论你是iOS开发的新手还是有经验的开发者,本文都将为你提供宝贵的见解和实用的指导。
145 66
|
2月前
|
存储 监控 API
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
90 11
|
2月前
|
开发框架 Android开发 iOS开发
安卓与iOS开发中的跨平台策略:一次编码,多平台部署
在移动应用开发的广阔天地中,安卓和iOS两大阵营各占一方。随着技术的发展,跨平台开发框架应运而生,它们承诺着“一次编码,到处运行”的便捷。本文将深入探讨跨平台开发的现状、挑战以及未来趋势,同时通过代码示例揭示跨平台工具的实际运用。
173 3
|
2月前
|
Java 调度 Android开发
安卓与iOS开发中的线程管理差异解析
在移动应用开发的广阔天地中,安卓和iOS两大平台各自拥有独特的魅力。如同东西方文化的差异,它们在处理多线程任务时也展现出不同的哲学。本文将带你穿梭于这两个平台之间,比较它们在线程管理上的核心理念、实现方式及性能考量,助你成为跨平台的编程高手。
|
3月前
|
存储 前端开发 Swift
探索iOS开发:从新手到专家的旅程
本文将带您领略iOS开发的奇妙之旅,从基础概念的理解到高级技巧的掌握,逐步深入iOS的世界。文章不仅分享技术知识,还鼓励读者在编程之路上保持好奇心和创新精神,实现个人成长与技术突破。
|
物联网 Android开发 iOS开发
iOS开发 - 蓝牙学习的总结
iOS开发 - 蓝牙学习的总结
204 0
|
iOS开发
IOS开发---菜鸟学习之路--(九)-利用PullingRefreshTableView实现下拉刷新
本章主要讲解如何利用PullingRefreshTableView实现下拉(上拉)刷新的操作  PullingRefreshTableView 实现上下拉刷新的例子百度有很多,大家可以自己搜索下,先看下那些例子(一般搜索过来的都是一样的大家反正先把那部分内容先了解一下,然后再看本文档比较好。
898 0
|
iOS开发 Android开发 存储
IOS开发---菜鸟学习之路--(十)-实现新闻详细信息浏览页面
前面已经将了上下拉刷新 实现了上下拉刷新后我们的第一级界面就做好,接下来我们就需要实现 新闻详细信息浏览了 我个人认为一般实现新闻详细页面的方法有两种(主要是数据源的不同导致了方法的不同) 第一种是本身新闻就是一个链接地址,同时是已经处理好的适应手机浏览的网页 对于这种类型的数据源,我们直接在页面中放一个WebView控件,然后将URL传递过去就好了 另一种则是普通的包含标题、时间、内容、图片等数据结构的新闻内容(我们要实现的也是这种新闻,因为实现了这种之后, 我们就可以实现任何自定义的详细信息的页面了。
897 0

热门文章

最新文章

  • 1
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 2
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 3
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 4
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 5
    用自然语言控制电脑,字节跳动开源 UI-TARS 的桌面版应用!内附详细的安装和配置教程
  • 6
    UI-TARS:字节跳动开源专注于多平台 GUI 自动化交互的视觉语言模型
  • 7
    【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 8
    移动端UI名词 - AxureMost
  • 9
    【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
  • 10
    unity判断鼠标在不在UI上