UITableView使用
2016-01-07 13:33
543 查看
性能优化
/***
每当有一个cell要进入视野范围内,就会调用一次
*/
- (UITableViewCell
*)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
static
NSString
*ID = @"wine";
// 1.先去缓存池中查找可循环利用的cell
UITableViewCell
*cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.如果缓存池中没有可循环利用的cell
if
(!cell) {
cell = [[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:ID];
// 尽量将cell的初始化设置,放在这个代码块中
// 如果这个设置是所有cell都要保持一致的,就可以放在这个代码块中
cell.textLabel.font
= [UIFont
systemFontOfSize:30];
cell.backgroundColor
= [UIColor
redColor];
}
// 3.设置数据
cell.textLabel.text
= [NSString
stringWithFormat:@"%zd行的数据",
indexPath.row];
if
(indexPath.row
% 2
== 0)
{
cell.accessoryType
= UITableViewCellAccessoryDisclosureIndicator;
}
else
{
cell.accessoryType
= UITableViewCellAccessoryNone;
}
return
cell;
}
索引条
/** * 返回每一组的索引标题(数组中放的是字符串) */- (NSArray
*)sectionIndexTitlesForTableView:(UITableView
*)tableView{
return
[self.carGroups
valueForKeyPath:@"title"];
}
如何让tableView展示数据
设置数据源对象self.tableView.dataSource
= self;
数据源对象要遵守协议
@interface
ViewController () <UITableViewDataSource>
@end
实现数据源方法
// 多少组数据
-
(NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView;
//
每一组有多少行数据
-
(NSInteger)tableView:(UITableView
*)tableView numberOfRowsInSection:(NSInteger)section;
//
每一行显示什么内容
-
(UITableViewCell
*)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath;
//
每一组的头部
-
(NSString
*)tableView:(UITableView
*)tableView titleForHeaderInSection:(NSInteger)section;
//
每一组的尾部
-
(NSString
*)tableView:(UITableView
*)tableView titleForFooterInSection:(NSInteger)section
tableView的常见设置
tableview的点击后,点击效果立刻消失-(void)tableView:(UITableView
*)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
[tableView
deselectRowAtIndexPath:indexPath animated:YES];
}
样式设置:
//
设置每一行cell的高度
self.tableView.rowHeight
= 100;
//
设置每一组头部的高度
self.tableView.sectionHeaderHeight
= 50;
//
设置每一组尾部的高度
self.tableView.sectionFooterHeight
= 50;
//
设置分割线颜色
self.tableView.separatorColor
= [UIColor
redColor];
//
设置分割线样式
self.tableView.separatorStyle
= UITableViewCellSeparatorStyleNone;
//
设置表头控件
self.tableView.tableHeaderView
= [[UISwitch
alloc] init];
//
设置表尾控件
self.tableView.tableFooterView
= [UIButton
buttonWithType:UIButtonTypeContactAdd];
//
设置右边索引文字的颜色
self.tableView.sectionIndexColor
= [UIColor
redColor];
//
设置右边索引文字的背景色
self.tableView.sectionIndexBackgroundColor
= [UIColor
blackColor];
tableViewCell的常见设置
// 设置右边的指示样式cell.accessoryType
= UITableViewCellAccessoryDisclosureIndicator;
//
设置右边的指示控件
cell.accessoryView
= [[UISwitch
alloc] init];
//
设置cell的选中样式
cell.selectionStyle
= UITableViewCellSelectionStyleNone;
//
backgroundView优先级 > backgroundColor
//
设置背景色
cell.backgroundColor
= [UIColor
redColor];
//
设置背景view
UIView
*bg = [[UIView
alloc] init];
bg.backgroundColor
= [UIColor
blueColor];
cell.backgroundView
= bg;
//
设置选中的背景view
UIView
*selectedBg = [[UIView
alloc] init];selectedBg.backgroundColor
= [UIColor
purpleColor];cell.selectedBackgroundView
= selectedBg;
cell的循环利用
传统的写法/** * 每当有一个cell要进入视野范围内,就会调用一次 */
-
(UITableViewCell
*)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
static
NSString
*ID = @"wine";
//
1.先去缓存池中查找可循环利用的cell
UITableViewCell
*cell = [tableView dequeueReusableCellWithIdentifier:ID];
//
2.如果缓存池中没有可循环利用的cell
if
(!cell) {
cell
= [[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:ID];
}
//
3.设置数据
cell.textLabel.text
= [NSString
stringWithFormat:@"%zd行的数据",
indexPath.row];
return
cell;
}
新的写法(注册cell)
NSString
*ID = @"wine";
-
(void)viewDidLoad
{
[super
viewDidLoad];
//
注册某个重用标识 对应的 Cell类型
[self.tableView
registerClass:[UITableViewCell
class] forCellReuseIdentifier:ID];
}
-
(UITableViewCell
*)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
//
1.先去缓存池中查找可循环利用的cell
UITableViewCell
*cell = [tableView dequeueReusableCellWithIdentifier:ID];
//
2.设置数据
cell.textLabel.text
= [NSString
stringWithFormat:@"%zd行的数据",
indexPath.row];
return
cell;
}
相关文章推荐
- UITableView 属性集合
- iOS下 UILabel 如何自动换行
- 处理二值查询 Processing Boolean queries
- 3.UIImageView基本使用
- 如何实现一个搜索引擎(2):查询索引 How to Implement a Search Engine Part 2: Query Index
- 【转载】GUID vs INT Debate
- 2.UILabel的基本使用
- Java源码-AbstractStringBuilder
- UITableView中复用cell显示信息错乱
- NSInteger 与 NSUInteger 和 int与 NSInteger 区别
- 基于Elasticsearch+Fluentd+Kibana的日志收集分析系统搭建与应用
- UICollectionView
- django实现用户登陆访问限制@login_required
- 1.大神班 UI 526
- 设计模式Builder模式
- iOS开发之UIImagePickerController图片选择管理器
- Android子线程真的不能更新UI么
- iOS个人整理03-UIButton按钮
- UILabel文字过长时的显示格式
- valueForKeyPath的用法(本篇内容是拷贝别人的文章,因为不能转载,又感觉写的很好,所以复制了,希望作者不要见怪)