iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)

简介: iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一、项目结构和plist文件   二、实现代码 1.说明: 主控制器直接继承UITableViewController // YYViewController.

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

 

目录
相关文章
|
3天前
|
Java 开发工具 Android开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
在移动应用开发的广阔天地中,Android和iOS两大平台各自占据着半壁江山。本文将深入探讨这两个平台在开发过程中的关键差异点,包括编程语言、开发工具、用户界面设计、性能优化以及市场覆盖等方面。通过对这些关键因素的比较分析,旨在为开发者提供一个清晰的指南,帮助他们根据项目需求和目标受众做出明智的平台选择。
|
3天前
|
编解码 Android开发 iOS开发
深入探索Android与iOS开发的差异与挑战
【6月更文挑战第24天】在移动应用开发的广阔舞台上,Android和iOS两大操作系统扮演着主角。它们各自拥有独特的开发环境、工具集、用户基础及市场策略。本文将深度剖析这两个平台的开发差异,并探讨开发者面临的挑战,旨在为即将踏入或已在移动开发领域奋斗的开发者提供一份实用指南。
26 13
|
6天前
|
iOS开发 开发者 容器
探索iOS开发中的SwiftUI框架
【6月更文挑战第21天】本文深入探讨了苹果在iOS开发中推出的SwiftUI框架,旨在为开发者提供一种声明式、更简洁的界面设计方法。文章首先概述了SwiftUI的核心概念和优势,接着通过一个天气预报应用实例,详细讲解了如何使用SwiftUI进行布局和用户界面的设计。此外,还讨论了SwiftUI与UIKit的差异,以及如何将SwiftUI集成到现有的项目中。最后,文章展望了SwiftUI的未来发展方向,包括潜在的改进和新特性。
|
2天前
|
监控 Android开发 iOS开发
探索Android与iOS开发的差异:平台、工具和用户体验的比较
【6月更文挑战第25天】在移动应用开发的广阔天地中,Android和iOS两大平台各领风骚,它们在开发环境、工具选择及用户体验设计上展现出独特的风貌。本文将深入探讨这两个操作系统在技术实现、市场定位和用户交互方面的关键差异,旨在为开发者提供一个全景式的视图,帮助他们在面对项目决策时能够更加明智地选择适合自己项目需求的平台。
|
6天前
|
Java 开发工具 Android开发
安卓与iOS开发差异解析
【6月更文挑战第21天】本文旨在深入探讨安卓和iOS两大移动操作系统在应用开发过程中的主要差异。通过对比分析,揭示各自的设计哲学、编程语言选择、用户界面构建、性能优化策略以及发布流程的异同。文章将提供开发者视角下的实用信息,帮助他们更好地理解各自平台的特点和挑战,从而做出更明智的开发决策。
|
7天前
|
Java 开发工具 Android开发
探索安卓与iOS开发的核心差异
【6月更文挑战第20天】在移动应用开发的广阔天地中,安卓和iOS两大平台各自占据半壁江山。本文将深入探讨这两大操作系统在开发过程中的主要区别,包括编程语言、开发工具、用户界面设计哲学、系统架构以及市场分布等方面。通过对这些关键差异的分析,旨在为开发者提供一份实用的指南,帮助他们在面对项目决策时,能够更加明智地选择合适的平台,并针对特定平台优化他们的应用。
|
20小时前
|
安全 Android开发 iOS开发
探索安卓与iOS开发的差异:平台特性与用户体验的深度对比
在移动应用开发的广阔天地中,安卓和iOS两大平台各占半壁江山。本文旨在通过数据驱动的分析方法,深入探讨这两大操作系统在开发环境、用户界面设计及市场表现等方面的差异。引用最新的行业报告和科研数据,结合技术专家的观点,本文将提供对开发者和市场分析师均有价值的洞见。
|
1天前
|
缓存 C语言 iOS开发
一篇文章讲明白iOS开发系列
一篇文章讲明白iOS开发系列
|
2天前
|
设计模式 IDE Swift
探索iOS开发:从新手到专家的旅程
【6月更文挑战第25天】在数字时代的浪潮中,iOS开发作为一门艺术和科学的结合体,吸引了众多开发者的目光。本文将带领读者踏上一场精彩的旅程,从基础的搭建环境开始,逐步深入到高级编程技巧,再到应用发布与市场策略,全方位解读iOS开发的魅力所在。通过实际案例分析,我们将揭示那些让应用脱颖而出的秘密,以及如何在竞争激烈的应用市场中保持竞争力。无论你是初学者还是有经验的开发者,这篇文章都将为你提供宝贵的见解和实用的技巧,让你的iOS开发之旅更加顺畅。
|
7天前
|
安全 Android开发 iOS开发
探索安卓与iOS开发的差异:平台特性与用户体验的对比分析
移动应用开发的两大阵营——安卓与iOS,各自拥有独特的开发环境、用户群体和市场定位。本文将深入探讨这两个操作系统在应用开发过程中的主要差异,包括编程语言、开发工具、用户界面设计、性能优化、安全性考量以及发布流程等方面。通过比较分析,旨在为开发者提供跨平台开发的见解和策略,以优化应用性能和提升用户体验。
11 0