tableView

1.如何利用tableView如何展示数据

设置数据源对象(一般是控制器)

self.tableView.dataSource = self;

 

数据源对象需要遵守协议->UITableViewDataSource

@interface ViewController ()<UITableViewDataSource>
@end

 

实现数据源协议里面的方法

/**
* 告诉tableView⼀一共有多少组
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
}
/**
* 告诉tableView第section组有多少⾏行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section
{
}
/**
* 告诉tableView每⼀一⾏行显⽰示的内容(tableView每⼀一⾏行都是UITableViewCell)
*/
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
/**
* 告诉tableView每⼀一组头部显⽰示的标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section
{
}
/**
* 告诉tableView每⼀一组尾部显⽰示的标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:
(NSInteger)section
{
}

 

2.tableView常见的设置

// 设置tableView每⼀一⾏行cell的⾼高度,默认是44
self.tableView.rowHeight = 80;
// 设置tableView每⼀一组头部的⾼高度
self.tableView.sectionHeaderHeight = 50;
// 设置tableView每⼀一组尾部的⾼高度
self.tableView.sectionFooterHeight = 50;
// 设置分割线的颜⾊色,如果设置[UIColor clearColor]隐藏分割线
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];

 

3.tableViewCell的常见设置

// 设置cell右边的指⽰示控件
cell.accessoryView = [[UISwitch alloc] init];
// 设置cell右边的指⽰示样式(accessoryView优先级 > accessoryType)
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 设置cell的背景view
// backgroundView优先级 > backgroundColor
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [UIColor blueColor];
cell.backgroundView = bg;
// 设置cell的背景颜⾊色
cell.backgroundColor = [UIColor redColor];
// 设置cell选中的背景view
UIView *selectbg = [[UIView alloc] init];
selectbg.backgroundColor = [UIColor purpleColor];
cell.selectedBackgroundView = selectbg;
// 设置cell选中的样式
cell.selectionStyle = UITableViewCellSelectionStyleNone;

 

4.代理方法

/**
* 当选中某⼀一⾏行cell就会调⽤用
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
}
/**
* 当取消选中某⼀一⾏行cell就会调⽤用
*/
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:
(NSIndexPath *)indexPath
{
}
/**
* 返回每⼀一组显⽰示的头部控件
*
*/
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:
(NSInteger)section
{
}
/**
* 返回每⼀一组显⽰示的尾部控件
*
*/
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:
(NSInteger)section
{
}
/**
* 返回每⼀一组头部的⾼高度
*
*/
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:
(NSInteger)section
{
}
/**
* 返回每⼀一组尾部的⾼高度
*
*/
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:
(NSInteger)section
{
}
/**
* 返回tableView每⼀一⾏行的⾼高度
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath
{
}

 

5.性能优化

传统的写法

/**
* 每当有⼀一个cell进⼊入视野范围内就会调⽤用1次
*/
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 0.定义⼀一个重⽤用标识
static NSString *ID = @"wine";
// 1.⾸首先去缓存池中查找可循环利⽤用的cell
UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:ID];
// 2.如果缓存池中没有,⾃自⼰己创建
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
//3. 设置数据
cell.textLabel.text = [NSString stringWithFormat:@"%zd⾏行数
据",indexPath.row];
return cell;
}

注册

- (void)viewDidLoad {
[super viewDidLoad];
// 根据ID 这个标识 注册对应的 cell类型
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:ID];
}
/**
* 每当有⼀一个cell进⼊入视野范围内就会调⽤用1次
*/
- (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;
}

 

6.索引条

/**
* 返回每⼀一组的索引标题(数组中都是NSString对象)
*/
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView
*)tableView
{
}

 

posted @ 2017-05-15 19:38  iFat  阅读(145)  评论(0编辑  收藏  举报