【UI初级--连载八】-------------表视图(UITableView)
2015-09-24 20:45
676 查看
1、总结一:storyBoard使用步骤及注意事项(注册单元格)
2、总结二:tableView属性和方法
3、总结三:tableView常用数据源委托方法(dataSource)
4、总结四:tableView常用代理委托方法(delegate)
总结一:使用storyBoard
(注:_tableView.dataSource = self;
_tableView.delegate = self; (在storyBoard中连线))
1、不需要NavigationController
在storyBoard中:拖个TableView至viewController,托代理,拖DataSource
2、需要NavigationController
在storyBoard中:
(1)拖NavigationController(删掉自带的RootViewController),is(入口)
(2)连线NavigationController和viewController(Root)
(3)拖个tableView至viewController(还需托代理)
(4)在.m文件中实现代理方法
(4.1)返回单元格方法中(单元格复用)
(4.2)拖table View cell (会有Prototype cells),
此时返回单元格方法中(单元格复用
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier: forIndexPath: ];)
(4.2.1)(xib)中,选中cell, 设置identifier
(4.2.2)在.m文件中 viewDidLoad方法中,在注册一个单元格(UI07-06)
( //注册一个单元格
(通过类去注册)---(不建议)
[tableView
registerClass:[UITableViewCell
class]
forCellReuseIdentifier:@"Cell"];)
(5)如果从别的ViewController弹出本视图控制器的话,弹出时需用到以下方法:
//拿到storyboard当中创建的控制器对象
UIStoryboard *storyboard = [UIStoryboard
storyboardWithName:@"Main"
bundle:nil];
NewsDetailViewController *detailVC = [storyboard
instantiateViewControllerWithIdentifier:@"NewsDetailVC"];
//push进入图片详情界面
[self.navigationController
pushViewController:detailVC
animated:YES];
总结二:tableView属性和方法
【1】创建
UITableView *tableView = [[UITableView alloc] initWithFrame: style: ];
//frame设置方法一:
CGRectMake(0,
20, [UIScreen
mainScreen].bounds.size.width,
[UIScreen
mainScreen].bounds.size.height
-
20)
//frame设置方法二:
self.view.bounds
//设置tableView的内容偏移量
tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
【2】属性(tableView的属性)
《1》分割线
(1)分割线风格---------------------separatorStyle
(2)分割线颜色---------------------separatorColor,默认标准灰色
(3)分割线偏移量-------------------separatorInset
(上、左、下、右)
//注意:分割线的左侧的偏移量用这个方法不能设置
_tableView.separatorInset
=
UIEdgeInsetsMake(0, -100,
0,
10);
《2》 头视图------------------------tableHeaderView
尾视图------------------------tableFooterView
//注意:头视图和尾视图的宽度手动设置无效,他会自动与屏幕齐宽
《3》单元格的行高
------------------rowHeight
--默认44
如果需要动态地确定行高需要在代理方法中实现
《4》section头视图行高-------------sectionHeaderHeight
section尾视图行高-------------sectionFooterHeight
《5》表示图背景---------------------backgroundView
他有时会影响你的背景设置,此时将其设为nil;
【3】方法(tableView的方法)
《1》刷新数据------------------------- reloadData
《2》根据indexPath,返回cell实例
------------------------------cellForRowAtIndexPath
《3》根据显示的cell,返回cell实例的数组
-------------------------------visibleCells
《4》根据显示的cell,返回NSIndexPath实例的数组
------------------------------indexPathsForVisibleRows
《5》根据组、行的下标,构建其IndexPath
NSIndexPath *indexpath = [NSIndexPath
indexPathForRow:2
inSection:0];
《6》使数据显示到指定位置,可以配置动画(需先构建IndexPath)
[self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition:
animated:YES];
《7》刷新组数据:---reloadSections:
withRowAnimation:
NSIndexSet *indexSet = [NSIndexSet
indexSetWithIndex:section];
[self.tableView
reloadSections:indexSet
withRowAnimation:UITableViewRowAnimationFade];
//自适应高度
cell.textLabel.numberOfLines
=
0;
总结三:tableView常用数据源委托方法(dataSource)<凡与数据相关的>
@required
// 返回多少行
- (NSInteger)tableView:(UITableView
*)tableView numberOfRowsInSection:(NSInteger)section;
// 单元格对象
- (UITableViewCell
*)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath;
@optional
// 返回多少组(默认为1)
- (NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView;
//组的头视图标题
- (NSString
*)tableView:(UITableView
*)tableView titleForHeaderInSection:(NSInteger)section;
// 组的尾视图标题
- (NSString
*)tableView:(UITableView
*)tableView titleForFooterInSection:(NSInteger)section;
总结四:tableView常用代理委托方法(delegate)<凡与UI相关的>
@optional
// Variable height support
//单元格行高
- (CGFloat)tableView:(UITableView
*)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath;
//头视图行高
- (CGFloat)tableView:(UITableView
*)tableView heightForHeaderInSection:(NSInteger)section;
//尾视图行高
- (CGFloat)tableView:(UITableView
*)tableView heightForFooterInSection:(NSInteger)section;
// 自定义头/尾视图
- (UIView
*)tableView:(UITableView
*)tableView viewForHeaderInSection:(NSInteger)section;
- (UIView
*)tableView:(UITableView
*)tableView viewForFooterInSection:(NSInteger)section;
// 当单元格被选中时
- (void)tableView:(UITableView
*)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath;
//当单元格即将出现时调用的方法
- (void)tableView:(UITableView
*)tableView willDisplayCell:(UITableViewCell
*)cell forRowAtIndexPath:(NSIndexPath
*)indexPath;
//当头视图即将出现时调用的方法
- (void)tableView:(UITableView
*)tableView willDisplayHeaderView:(UIView
*)view forSection:(NSInteger)section
NS_AVAILABLE_IOS(6_0);
//当尾视图即将出现时调用的方法
- (void)tableView:(UITableView
*)tableView willDisplayFooterView:(UIView
*)view forSection:(NSInteger)section
NS_AVAILABLE_IOS(6_0);
//当单元格即将消失时调用的方法
- (void)tableView:(UITableView
*)tableView didEndDisplayingCell:(UITableViewCell
*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
NS_AVAILABLE_IOS(6_0);
//当头视图即将消失时调用的方法
- (void)tableView:(UITableView
*)tableView didEndDisplayingHeaderView:(UIView
*)view forSection:(NSInteger)section
NS_AVAILABLE_IOS(6_0);
//当尾视图即将消失时调用的方法
- (void)tableView:(UITableView
*)tableView didEndDisplayingFooterView:(UIView
*)view forSection:(NSInteger)section
NS_AVAILABLE_IOS(6_0);
2、总结二:tableView属性和方法
3、总结三:tableView常用数据源委托方法(dataSource)
4、总结四:tableView常用代理委托方法(delegate)
总结一:使用storyBoard
(注:_tableView.dataSource = self;
_tableView.delegate = self; (在storyBoard中连线))
1、不需要NavigationController
在storyBoard中:拖个TableView至viewController,托代理,拖DataSource
2、需要NavigationController
在storyBoard中:
(1)拖NavigationController(删掉自带的RootViewController),is(入口)
(2)连线NavigationController和viewController(Root)
(3)拖个tableView至viewController(还需托代理)
(4)在.m文件中实现代理方法
(4.1)返回单元格方法中(单元格复用)
(4.2)拖table View cell (会有Prototype cells),
此时返回单元格方法中(单元格复用
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier: forIndexPath: ];)
(4.2.1)(xib)中,选中cell, 设置identifier
(4.2.2)在.m文件中 viewDidLoad方法中,在注册一个单元格(UI07-06)
( //注册一个单元格
(通过类去注册)---(不建议)
[tableView
registerClass:[UITableViewCell
class]
forCellReuseIdentifier:@"Cell"];)
(5)如果从别的ViewController弹出本视图控制器的话,弹出时需用到以下方法:
//拿到storyboard当中创建的控制器对象
UIStoryboard *storyboard = [UIStoryboard
storyboardWithName:@"Main"
bundle:nil];
NewsDetailViewController *detailVC = [storyboard
instantiateViewControllerWithIdentifier:@"NewsDetailVC"];
//push进入图片详情界面
[self.navigationController
pushViewController:detailVC
animated:YES];
总结二:tableView属性和方法
【1】创建
UITableView *tableView = [[UITableView alloc] initWithFrame: style: ];
//frame设置方法一:
CGRectMake(0,
20, [UIScreen
mainScreen].bounds.size.width,
[UIScreen
mainScreen].bounds.size.height
-
20)
//frame设置方法二:
self.view.bounds
//设置tableView的内容偏移量
tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
【2】属性(tableView的属性)
《1》分割线
(1)分割线风格---------------------separatorStyle
(2)分割线颜色---------------------separatorColor,默认标准灰色
(3)分割线偏移量-------------------separatorInset
(上、左、下、右)
//注意:分割线的左侧的偏移量用这个方法不能设置
_tableView.separatorInset
=
UIEdgeInsetsMake(0, -100,
0,
10);
《2》 头视图------------------------tableHeaderView
尾视图------------------------tableFooterView
//注意:头视图和尾视图的宽度手动设置无效,他会自动与屏幕齐宽
《3》单元格的行高
------------------rowHeight
--默认44
如果需要动态地确定行高需要在代理方法中实现
《4》section头视图行高-------------sectionHeaderHeight
section尾视图行高-------------sectionFooterHeight
《5》表示图背景---------------------backgroundView
他有时会影响你的背景设置,此时将其设为nil;
【3】方法(tableView的方法)
《1》刷新数据------------------------- reloadData
《2》根据indexPath,返回cell实例
------------------------------cellForRowAtIndexPath
《3》根据显示的cell,返回cell实例的数组
-------------------------------visibleCells
《4》根据显示的cell,返回NSIndexPath实例的数组
------------------------------indexPathsForVisibleRows
《5》根据组、行的下标,构建其IndexPath
NSIndexPath *indexpath = [NSIndexPath
indexPathForRow:2
inSection:0];
《6》使数据显示到指定位置,可以配置动画(需先构建IndexPath)
[self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition:
animated:YES];
《7》刷新组数据:---reloadSections:
withRowAnimation:
NSIndexSet *indexSet = [NSIndexSet
indexSetWithIndex:section];
[self.tableView
reloadSections:indexSet
withRowAnimation:UITableViewRowAnimationFade];
//自适应高度
cell.textLabel.numberOfLines
=
0;
总结三:tableView常用数据源委托方法(dataSource)<凡与数据相关的>
@required
// 返回多少行
- (NSInteger)tableView:(UITableView
*)tableView numberOfRowsInSection:(NSInteger)section;
// 单元格对象
- (UITableViewCell
*)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath;
@optional
// 返回多少组(默认为1)
- (NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView;
//组的头视图标题
- (NSString
*)tableView:(UITableView
*)tableView titleForHeaderInSection:(NSInteger)section;
// 组的尾视图标题
- (NSString
*)tableView:(UITableView
*)tableView titleForFooterInSection:(NSInteger)section;
总结四:tableView常用代理委托方法(delegate)<凡与UI相关的>
@optional
// Variable height support
//单元格行高
- (CGFloat)tableView:(UITableView
*)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath;
//头视图行高
- (CGFloat)tableView:(UITableView
*)tableView heightForHeaderInSection:(NSInteger)section;
//尾视图行高
- (CGFloat)tableView:(UITableView
*)tableView heightForFooterInSection:(NSInteger)section;
// 自定义头/尾视图
- (UIView
*)tableView:(UITableView
*)tableView viewForHeaderInSection:(NSInteger)section;
- (UIView
*)tableView:(UITableView
*)tableView viewForFooterInSection:(NSInteger)section;
// 当单元格被选中时
- (void)tableView:(UITableView
*)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath;
//当单元格即将出现时调用的方法
- (void)tableView:(UITableView
*)tableView willDisplayCell:(UITableViewCell
*)cell forRowAtIndexPath:(NSIndexPath
*)indexPath;
//当头视图即将出现时调用的方法
- (void)tableView:(UITableView
*)tableView willDisplayHeaderView:(UIView
*)view forSection:(NSInteger)section
NS_AVAILABLE_IOS(6_0);
//当尾视图即将出现时调用的方法
- (void)tableView:(UITableView
*)tableView willDisplayFooterView:(UIView
*)view forSection:(NSInteger)section
NS_AVAILABLE_IOS(6_0);
//当单元格即将消失时调用的方法
- (void)tableView:(UITableView
*)tableView didEndDisplayingCell:(UITableViewCell
*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
NS_AVAILABLE_IOS(6_0);
//当头视图即将消失时调用的方法
- (void)tableView:(UITableView
*)tableView didEndDisplayingHeaderView:(UIView
*)view forSection:(NSInteger)section
NS_AVAILABLE_IOS(6_0);
//当尾视图即将消失时调用的方法
- (void)tableView:(UITableView
*)tableView didEndDisplayingFooterView:(UIView
*)view forSection:(NSInteger)section
NS_AVAILABLE_IOS(6_0);
相关文章推荐
- zoj 3820 Building Fire Stations 树的中心
- baguetteBox.js响应式画廊插件(纯JS)
- 【UI初级------连载七】标签控制器
- 【UIKit-124-11】#import <UIKit/UIView.h>
- POJ 3061 Subsequence (迟取法_预处理,连续子序列)
- Android samples API Demos之UI篇2(ActivitySceneTransitionBasic)
- unsigned int reverse_bit(unsigned int value)
- UIStepper
- PHP中require和include路径问题详解
- STL系列之一 deque双向队列
- 文顶顶 iOS开发UI篇—UITabBarController简单介绍 iOS开发UI篇—UITabBarController简单介绍
- UI小总结
- IDEA14 hibernate :createQuery("from UserEntity")Can't resolve symbol 'UserEntity' less..
- UIView显示图片
- Map、Set、List、Queue、Stack的特点与用法2
- UIWindow
- Map、Set、List、Queue、Stack的特点与用法1
- UEditor 编辑器跨域上传解决方法
- 第十九篇:猜图游戏UI完整代码
- iOS开发-------UIAlertController(弹出视图控制器)