您的位置:首页 > 产品设计 > UI/UE

UITableView

2015-11-12 17:47 239 查看
1、什么是TableView

* 就是在APP中看到的列表数据

* 在iOS中,要实现展示列表数据,最常用的做法就是使用UITableView

* UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳

2、 TableView的两种样式

UITableViewStylePlain (例如:汽车之家主界面)

UITableViewStyleGroupe (例如:微信的发现界面)

3、 如何展示数据

* UITableView需要一个数据源(dataSource)来显示数据

*UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等

#pragma mark - <UITableViewDataSource>
/**
* 告诉tableView一共有多少组
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return   2; // 随便写的
}

/**
*  告诉tableView第section组有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) return 2;
if (section == 1) return 5;
return 0;
}

/**
* 告诉tableView每一行显示什么内容(tableView的每一行都是UITableViewCell)
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = [[UITableViewCell alloc] init];

if (indexPath.section == 0) { // 第0组
if (indexPath.row == 0) { // 第0组第0行
cell.textLabel.text = @"通用";
} else if (indexPath.row == 1) { // 第0组第1行
cell.textLabel.text = @"隐私";
}
} else {
cell.textLabel.text = [NSString stringWithFormat:@"其他数据-%zd组%zd行", indexPath.section, indexPath.row];
}
return cell;
}


*没有设置数据源的UITableView只是个空壳

*凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源

@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end


TableView的常见属性

//1. 修改tableView的行高
self.tableView.rowHeight = 100;

// 2.组头组尾的高
self.tableView.sectionHeaderHeight = 55;
self.tableView.sectionFooterHeight = 22;

// 3.设置整个tablView的头部/尾部视图
self.tableView.tableHeaderView = [[UISwitch alloc] init];
self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeInfoDark];

// 4.设置我们分割线颜色(clearColor相当于取消系统分割线)
//self.tableView.separatorColor = [UIColor clearColor];

// 5.设置分割线样式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

// 设置索引条内部文字颜色为不纯洁颜色
self.tableView.sectionIndexColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
// 设置索引条背景颜色为纯洁颜色
self.tableView.sectionIndexBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];


TableView常见的代理方法:

// 当选中index行cell时候会调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

// 当取消选中cell时候调用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

// 返回对应行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

// 返回对应组的组头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

// 返回对应组头试图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: