iOS开发之UITabBarController和UICollectionView的使用
这一篇要记录的是iOS开发中UITabBarController控件和UICollectionView控件的使用。APP跑起来之后的效果例如以下图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
UITabBarController控件,即上图中页面下部能够切换到不同页面去的选项卡。UICollectionView即上右图中的View。以格子的形式展现出来。
本篇主要用到了例如以下几个知识点:
1、给ViewController加上UITabBarController控件
2、给ViewController加上UINavigationController控件
3、使用UITableView和UICollectionView展示列表
4、使用plist文件,在代码中读取plist文件并载入到UITableView和UICollectionView中
以下就一步步完毕这个项目:
1、首先在xcode中新建项目,取名为TabBarControllerTest
2、切换到Main.storyboard,然后选中ViewController视图,在菜单条中选择Editor-->Embed in-->Tab Bar Controller,这一步操作完毕后。故事板中的界面例如以下所看到的:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
3、以下给ViewController设置TabBar,在故事板中选中ViewController下方的TabBar。然后在xcode右側的属性栏中。设置System item为Contacts,例如以下图所看到的:
4、给ViewController加上导航栏。
选中故事板中的ViewController,然后在菜单条中选择Editor-->Embed in-->NavigationController,操作完毕后界面例如以下所看到的:
然后选中ViewController视图顶部的Navigation,在xcode右側的属性栏中配置title为“联系人”。
5、新增一个代表近期联系人的ViewController。
在控件区拖入一个ViewController到故事板中。然后鼠标右键按住。从Tab Bar Controller视图拖到刚刚新建的ViewController视图上,例如以下图所看到的:
松开鼠标后。在出现的对话框中,选择view controller,例如以下图所看到的:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
如今故事板中的视图是以下这样:
採用和上面的步骤同样的方法,选中我们新建的那个ViewController视图底部的TabBar。然后在xcode右側的视图中配置System item为Recents。
6、为新增的那个ViewController加上导航栏。
选中故事板中我们新增的ViewController,然后在菜单中选择Editor-->Embed in-->Navigation Controller,然后像前面的操作那样设置导航栏的标题为“近期联系人”。操作完毕后的故事板应该例如以下图所看到的:
到这里能够先执行程序看看效果。尽管没有数据显示,可是我们的TabBar还是能够切换的,切换到不同的Tab之后。导航栏上的标题也会随着切换。例如以下图所看到的:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
7、给页面中增加数据。
首先是联系人列表的数据,这个在前面的博文中已有说到,这里就不具体说了,这里我们须要新建一个单元格类ContactCell继承自UITableViewCell,ContactCell.h的代码例如以下:
#import <UIKit/UIKit.h> @interface ContactCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIImageView *avatarImage; @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @property (weak, nonatomic) IBOutlet UILabel *phoneLabel; @end
然后在ViewController中载入数据。ViewController.h文件的代码例如以下:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *tableView; @end
这里主要是ViewController.m中的代码。我们在当中载入了外部plist文件中的数据,先贴上ViewController.m文件的代码:
#import "ViewController.h"
#import "ContactCell.h" @interface ViewController () @end @implementation ViewController NSArray *avatarArr;
NSArray *nameArr;
NSArray *phoneArr;
static NSString *identifier = @"ContactCell"; - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self loadDataFromFile];
//设置tableView的代理和数据源
self.tableView.delegate = self;
self.tableView.dataSource = self;
} #pragma mark 从plist中载入数据
- (void)loadDataFromFile {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
avatarArr = [dict objectForKey:@"avatars"];
nameArr = [dict objectForKey:@"names"];
phoneArr = [dict objectForKey:@"phones"];
} #pragma mark 返回TableView的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [nameArr count];
} #pragma mark 返回某个单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ContactCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
cell.avatarImage.image = [UIImage imageNamed:[avatarArr objectAtIndex:indexPath.row]];
cell.nameLabel.text = [nameArr objectAtIndex:indexPath.row];
cell.phoneLabel.text = [phoneArr objectAtIndex:indexPath.row];
return cell;
} #pragma mark 返回单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80;
} #pragma mark 设置选中某个单元格后。背景色恢复初始状态
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
从plist文件里载入数据。主要使用的NSBundle类的pathForResource方法。这里我们的plist文件名称为contacts,文件内容例如以下:
8、给CollectionView载入数据
为了给CollectionView载入数据,首先得新建一个代表CollectionView单元格的类,这里取名为RecentCell,且这个类继承自UICollectionViewCell,RecentCell.h代码例如以下:
#import <UIKit/UIKit.h> @interface RecentCell : UICollectionViewCell @property (weak, nonatomic) IBOutlet UIImageView *avatarImage; @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @end
然后还得新建一个装载了CollectionView的ViewController,这里取名为RecentViewController,RecentViewController.h代码例如以下:
#import <UIKit/UIKit.h> @interface RecentViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate> @property (weak, nonatomic) IBOutlet UICollectionView *recentCollectionView; @end
里面仅包括一个代表CollectionView的变量,RecentViewController.m文件的代码例如以下:
#import "RecentViewController.h"
#import "RecentCell.h" @interface RecentViewController () @end @implementation RecentViewController NSArray *recentAvatarArr;
NSArray *recentNameArr;
static NSString *identifier = @"RecentCell"; - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self loadDataFromFile];
self.recentCollectionView.delegate = self;
self.recentCollectionView.dataSource = self;
} #pragma mark 从文件里载入数据
- (void)loadDataFromFile {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
recentAvatarArr = [dict objectForKey:@"avatars"];
recentNameArr = [dict objectForKey:@"names"];
} #pragma mark 一个section中的item数目
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [recentAvatarArr count];
} #pragma mark 返回某个单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RecentCell *cell = [self.recentCollectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.avatarImage.image = [UIImage imageNamed:[recentAvatarArr objectAtIndex:indexPath.row]];
cell.nameLabel.text = [recentNameArr objectAtIndex:indexPath.row];
return cell;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
事实上UICollectionView的使用方法和UITableView的使用方法类似,都是须要在头文件里实现数据源和托付协议。然后在.m文件里实现协议里的某几个方法来处理视图中显示的数据。
以下总结下在项目过程中须要注意的点:
(1)要注意在xcode的属性视图中给单元格指定的标识一定要和代码中的相应
(2)不要忘了将代码中声明的代表视图对象的变量,和真正的视图联系起来
(3)要给UITableView和UICollectionView等集合视图加入数据。一定不要忘了在头文件里实现协议
iOS开发之UITabBarController和UICollectionView的使用的更多相关文章
- IOS开发之UITabBarController与UINavigationController混合使用
ios开发中UITabBarController与UINavigationController混合使用是很多app的基础页面结构,下面是简单的的页面初始化的方法,在AppDelegate.m的 - ( ...
- iOS开发之UITabBarController
1.概述 跟UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型例子就是QQ.微信等应用. 2.UITabB ...
- iOS开发之Socket通信实战--Request请求数据包编码模块
实际上在iOS很多应用开发中,大部分用的网络通信都是http/https协议,除非有特殊的需求会用到Socket网络协议进行网络数 据传输,这时候在iOS客户端就需要很好的第三方CocoaAsyncS ...
- iOS开发之UISearchBar初探
iOS开发之UISearchBar初探 UISearchBar也是iOS开发常用控件之一,点进去看看里面的属性barStyle.text.placeholder等等.但是这些属性显然不足矣满足我们的开 ...
- iOS开发之UIImage等比缩放
iOS开发之UIImage等比缩放 评论功能真不错 评论开通后,果然有很多人吐槽.谢谢大家的支持和关爱,如果有做的不到的地方,还请海涵.毕竟我一个人的力量是有限的,我会尽自己最大的努力大家准备一些干货 ...
- iOS开发之 Xcode6 添加xib文件,去掉storyboard的hello world应用
iOS开发之 Xcode6.1创建仅xib文件,无storyboard的hello world应用 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载原理 ...
- iOS开发之loadView、viewDidLoad及viewDidUnload的关系
iOS开发之loadView.viewDidLoad及viewDidUnload的关系 iOS开发之loadView.viewDidLoad及viewDidUnload的关系 标题中所说的3个方 ...
- iOS开发之info.pist文件和.pch文件
iOS开发之info.pist文件和.pch文件 如果你是iOS开发初学者,不用过多的关注项目中各个文件的作用.因为iOS开发的学习路线起点不在这里,这些文件只会给你学习带来困扰. 打开一个项目,我们 ...
- iOS开发之WKWebView简单使用
iOS开发之WKWebView简单使用 iOS开发之 WKWebVeiw使用 想用UIWebVeiw做的,但是突然想起来在iOS8中出了一个新的WKWebView,算是UIWebVeiw的升级版. ...
随机推荐
- 【http】【转发】HTTP访问控制(CORS)
当一个资源从与该资源本身所在的服务器不同的域或端口请求一个资源时,资源会发起一个跨域 HTTP 请求. 比如,站点 http://domain-a.com 的某 HTML 页面通过 <img ...
- PAT Basic 1003
1003 我要通过! “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答 ...
- 【ORACLE】调整序列的当前种子值
[ORACLE]调整序列的当前种子值 --必须用SYS用户执行脚本:或具有SYSDBA角色登录: CREATE OR replace ); v_step ):;--步进 tsql ); BEGIN E ...
- IntelliJ IDEA 代码提示快捷键
1.写代码时用Alt-Insert(Code|Generate…)可以创建类里面任何字段的getter与setter方法. mac版 是ctrl+enter 2.CodeCompletion(代码完成 ...
- arc和mrc混用
arc项目中引用非arc代码 加上“-fno-objc-arc” 非arc项目中引用arc代码 加上“-fobjc-arc”
- VIJOS 1889 天真的因数分解 ——莫比乌斯函数
同理BZOJ2440 二分答案,不过这次变成了统计含有平方因子的个数 #include <cmath> #include <cstdio> #include <cstri ...
- 在 Linux 实例上自动安装并运行 VNC Server
原文网址:https://help.aliyun.com/knowledge_detail/41181.html?spm=5176.8208715.110.11.4c184ae8mlC7Yy 您可以使 ...
- Win10下安装虚拟机提示“Intel VT-x处于禁用状态”如何解决
今天在自己的笔记电脑上想安装一个Kali虚拟机学习用,安装的时候遇到这个问题,提示"Intel VT-x处于禁用状态". 要进入Win10的BIOS,先进入"设置&quo ...
- uva 11806 容斥原理+dfs
In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...
- HDU [P3949] XOR
线性基求第 k 小异或值 http://www.cnblogs.com/Mr-WolframsMgcBox/p/8567844.html 这道题消元下来是一个上三角矩阵,代码简单,但是不使用与本题的情 ...