iOS UITableView的用法
2016-04-09 15:33
281 查看
什么是UITableView
1.它是继承自UIScrollView的,所以它支持滚动,但是只支持垂直滚动
2.它有两种样式,就是分组(UITableViewStylePlain)不分组(UITableViewStyleGrouped)
3.通过设置UITableView的数据源(DataSource)来显示数据
在代码中或在视图上设置好数据源后,就要实现数据源的方法
#pragma -mark tableView数据源
必须要实现的有两个方法
//这个方法返回的是这个tableView的各个组里有多少行数据,组数的获取是传入的section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//这个方法返回的是这个tableView的每个cell需要具体显示什么数据,这个方法会在每次创建cell时调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath;
可选实现的有下面的这些方法
//返回的这个tableView分了多少组,如果不分组的话,这个方法没有什么意义
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//设置组的头标题
- (nullable
NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//设置组的尾标题
- (nullable
NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
#pragma -mark cell的使用
cell中可以设置3种内容来显示数据
textLabel,detailLabel,imageView
cell的四种style
UITableViewCellStyleDefault,UITableViewCellStyleValue1,UITableViewCellStyleValue2,UITableViewCellStyleSubtitle
-----如何去设置cell的高度
1.通过self.tableView.rowHeight 可以为所有cell设置高度,但是只能设置相同的高度
2.若想为tableView的cell设置不同的高度,就要实现tableView的代理啦
#pragma mark -tableView的代理
tableView的代理都是可选实现的,
//这个就是为不同的cell设置不同的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath;
-------cell的重用
当cell出屏幕的时候,把cell对象存储在特定标示的缓存池中,当tableView加载下一个数据,从缓存池中取出空闲的cell
static NSString *reuseID =
@"ID";//每次调用的时候防止都会创建对象,所以延长对象的生命周期
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:reuseID];
if(cell ==
nil) {
cell = [[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:reuseID];
//不能再次设置cell的属性
}
1.它是继承自UIScrollView的,所以它支持滚动,但是只支持垂直滚动
2.它有两种样式,就是分组(UITableViewStylePlain)不分组(UITableViewStyleGrouped)
3.通过设置UITableView的数据源(DataSource)来显示数据
在代码中或在视图上设置好数据源后,就要实现数据源的方法
#pragma -mark tableView数据源
必须要实现的有两个方法
//这个方法返回的是这个tableView的各个组里有多少行数据,组数的获取是传入的section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//这个方法返回的是这个tableView的每个cell需要具体显示什么数据,这个方法会在每次创建cell时调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath;
可选实现的有下面的这些方法
//返回的这个tableView分了多少组,如果不分组的话,这个方法没有什么意义
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//设置组的头标题
- (nullable
NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//设置组的尾标题
- (nullable
NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
#pragma -mark cell的使用
cell中可以设置3种内容来显示数据
textLabel,detailLabel,imageView
cell的四种style
UITableViewCellStyleDefault,UITableViewCellStyleValue1,UITableViewCellStyleValue2,UITableViewCellStyleSubtitle
-----如何去设置cell的高度
1.通过self.tableView.rowHeight 可以为所有cell设置高度,但是只能设置相同的高度
2.若想为tableView的cell设置不同的高度,就要实现tableView的代理啦
#pragma mark -tableView的代理
tableView的代理都是可选实现的,
//这个就是为不同的cell设置不同的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath;
-------cell的重用
当cell出屏幕的时候,把cell对象存储在特定标示的缓存池中,当tableView加载下一个数据,从缓存池中取出空闲的cell
static NSString *reuseID =
@"ID";//每次调用的时候防止都会创建对象,所以延长对象的生命周期
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:reuseID];
if(cell ==
nil) {
cell = [[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:reuseID];
//不能再次设置cell的属性
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- iOS推送的那些事