iOS编程(2)TableViewController
2016-06-05 13:24
465 查看
一、介绍
这次主要做了一个TableViewController,其中TableViewController是用代码编写的,而TableView中的Cell则是用xib文件做的,接下来简单介绍一下我做的
二、工作
之前用TabBarController建立了分页后,每个分页需要展示不同的内容,比如“我”,需要展示个人信息,所以用TableView来实现
关于TableView我觉得主要有这些是要注意的,Cell,Delegate以及Data Source。UITableViewController的使用可以参考以下官方文档UITableView
三、实现
1、首先需要构建Cell,这里我用xib的方法构建,同时用以下代码登录到TableView
[self.tableView registerNib:[UINib nibWithNibName:@"MeHeadPhotoCell" bundle:nil] forCellReuseIdentifier:meHeadPhotoCellIdentifier];
下图是register的Cell的xib文件
2、Data Source主要告诉TableView关于内容方面的信息,比如有多少个section和每个section有多少row等,还有比较重要的就是告诉TableView在某个IndexPath下的Cell长什么样,主要是以下这几个函数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
比如说贴出来的TableView的图,就有5个section,每个section之间有Header和Footer间隔开,其中第三个section有3个row,其他均为1个row
3、Delegate这里用到主要是告诉TableView每个Cell的高度,每个section之间的距离,点击某个Cell后的反应等等,用到以下函数
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
四、微小的工作
(1)在registerNib的时候nibWithNibName需要跟构建的xib的文件名字一样,而在没有xib文件时用registerClass达到一样的register的效果
(2)Button,TextField等组件可以设置圆角,用xx.layer.cornerRadius来设置,同时还需要设置xx.layer.masksToBounds成YES来显示
(3)左上角是坐标是(0,0),屏幕长宽可以用[UIScreen mainScreen].bounds.size.width和[UIScreen mainScreen].bounds.size.height来获得
(4)圆括号是类别的意思,可以扩展原来的类比如
ButtonColor.h @interface UIColor (buttonColor) + (instancetype)logoutButtonColor; + (instancetype)logoutButtonTouchUpInsideColor; + (instancetype)backgroundColor; + (instancetype)textFieldBorderColor; + (instancetype)remindMessageGrayColor; + (instancetype)loginAndFindPasswordGrayColor; @end ButtonColor.m @implementation UIColor (buttonColor) + (instancetype)logoutButtonColor { return [UIColor colorWithRed:234.0/255.0 green:65.0/255.0 blue:107.0/255.0 alpha:1.0]; } + (instancetype)logoutButtonTouchUpInsideColor { return [UIColor colorWithRed:126.0/255.0 green:55.0/255.0 blue:92.0/255.0 alpha:1.0]; } + (instancetype)backgroundColor { return [UIColor colorWithRed:237.0/255.0 green:239.0/255.0 blue:240.0/255.0 alpha:1.0]; } + (instancetype)textFieldBorderColor { return [UIColor colorWithRed:231.0/255.0 green:233.0/255.0 blue:232.0/255.0 alpha:1.0]; } + (instancetype)remindMessageGrayColor { return [UIColor colorWithRed:182.0/255.0 green:183.0/255.0 blue:185.0/255.0 alpha:1.0]; } + (instancetype)loginAndFindPasswordGrayColor; { return [UIColor colorWithRed:58.0/255.0 green:60.0/255.0 blue:62.0/255.0 alpha:1.0]; } @end
(5)一般设置常数,在类别前用const,比如const int,而NSString*的const只能放在NSString后面。因为NSString*本身就是不可改变的,所以const NSString*是冗余的,如果想说明pointer本身也是不能变的,就把const放在NSString*后面
(6)Delegate和DataSource,简单的区别,源自官方文档:A data source is like a delegate except that, instead of being delegated control of the user interface, it is delegated control of data.而Delegate是:A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program.具体可看Delegate&DataSource
相关文章推荐
- iOS开发实战——CollectionView点击事件与键盘隐藏结合案例
- ios-实战项目笔记总结-商城类-导航控制器
- 对一个类里面的数组添加观察时注意问题
- IOS文本框事件--委托方式实现
- 非3D Touch的设备上模拟Peek Preview效果
- 关于iOS socket
- iOS 对数组排序
- iOS开发中常用第三方库的整理
- iOS 点击按钮 闪光
- ios 多线程——原子、非原子,自旋锁和互斥锁
- iOS Autolayout 介绍
- 内存拷贝渲染视频的研究
- iOS Autolayout 介绍
- ios 实现自定义状态栏StatusBar 和 导航栏navigationBar 的状态和颜色背景图片
- iOS开发之监听键盘高度的变化
- iOS NSMutableAttributedString 富文本
- 问题:不能使用U盘或CD启动系统
- xcode7 iOS 9 NSURLSession 索引还有点问题
- iOS图片拉伸三种方式
- iOS 订单 物流接口数据的处理