首先我们需要明确UITableView和UITableViewController的概念。
UITableView是iOS中的一个视图控件,是显示列表数据的主要视图组件,通过UITableView可以方便的展示和管理大量的数据。
UITableViewController则是UIKit库中特定的视图控制器,主要作用是管理UITableView视图。UITableViewController提供了内存管理、数据源管理、滚动管理、编辑、行选中、重用单元格等常用功能,为处理UITableView提供了便利。
下面我们开始介绍iOS UITableView 与 UITableViewController实例详解,并附带两个示例。
一、UITableView实例详解
UITableView的使用流程一般为:创建UITableView→配置UITableView→实现数据源代理和UITableViewDelegate。下面简单介绍以下UITableView的实例详解。
1.创建UITableView
在UIViewController中新建一个UITableView对象,可以通过如下代码:
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
其中参数style为UITableView的样式,包括Plain和Grouped两种。
2.配置UITableView
UITableView最为常见的配置以及一些样式设置如下:
2.1 设置数据源和代理
UITableView必须实现UITableViewDataSource和UITableViewDelegate协议。
tableView.delegate = self;
tableView.dataSource = self;
设置UITableView的代理和数据源协议,其中delegate和datasource分别是UITableView的两个协议。
2.2 设置表格分割线
- 隐藏分割线:
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- 自定义分割线:
tableView.separatorColor = [UIColor redColor];//设置分割线的颜色
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//设置分割线样式
2.3 设置表头和表尾
tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
上面代码中tableView的tableHeaderView为UIView类型,通过UIView来实现我们自定义的表头。
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
上面代码中tableView的tableFooterView为UIView类型,通过UIView来实现我们自定义的表尾。
2.4 设置单元格部分属性
- 设置单元格高度:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
- 设置背景颜色:
cell.backgroundColor = [UIColor whiteColor];
- 处理选中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"选中了第%ld行", (long)indexPath.row);
}
- 设置滚动条颜色:
tableView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
3.实现数据源代理和UITableViewDelegate
UITableView的数据源代理委托类主要有以下几个方法:
- numberOfRowsInSection:返回对应的section有多少行。
- cellForRowAtindexPath:返回每行的单元格。
- numberOfSections:返回表格中有多少个section。
- titleForHeaderInSection:返回每个section的标题。
- viewForHeaderInSection:返回每个section的头部视图。
- viewForFooterInSection:返回每个section的尾部视图。
UITableViewDelegate委托类主要有以下几个方法:
- heightForRowAtIndexPath:每一行的高度。
- heightForFooterInSection:
- heightForHeaderInSection:
- willSelectRowAtIndexPath:
- didSelectRowAtIndexPath:选中该行的时候调用。
- willDeselectRowAtIndexPath:
- didDeselectRowAtIndexPath:取消选中该行的时候调用。
- willDisplayCell:在cell即将显示的时候调用。
示例一
下面是一个简单的UITableView示例,用于展示数据:
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"UITableView";
[self.view addSubview:self.tableView];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = self.dataSource[indexPath.row];
return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"选中了第%ld行", (long)indexPath.row);
}
#pragma mark - lazy
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
}
return _tableView;
}
- (NSArray *)dataSource {
if (!_dataSource) {
_dataSource = @[@"张三", @"李四", @"王五", @"赵六", @"钱七"];
}
return _dataSource;
}
@end
二、UITableViewController实例详解
UITableViewController的使用流程一般为:继承UITableViewController父类→设置UITableView的状态并实现数据源代理和UITableViewDelegate。下面简单介绍以下UITableViewController的实例详解。
1.继承UITableViewControler父类
通过继承UITableViewController,自带了一个tableview,并且重写了UITableViewDataSource和UITableViewDelegate两个协议方法。
@interface TableViewController : UITableViewController
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"UITableViewController";
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
cell.textLabel.text = [NSString stringWithFormat:@"这是第%ld行", (long)indexPath.row];
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
2.设置UITableView的状态并实现数据源代理和UITableViewDelegate
这时,需要设置tableview的属性,具体如下:
@interface TableViewController : UITableViewController
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"UITableViewController";
// 不显示分割线和滚动条
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.showsVerticalScrollIndicator = NO;
// 设置表格区头、区尾和行高
self.tableView.estimatedRowHeight = 80;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.sectionHeaderHeight = 0.1;
self.tableView.sectionFooterHeight = 10.0;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
cell.textLabel.text = [NSString stringWithFormat:@"这是第%ld行", (long)indexPath.row];
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
以上就是对iOS UITableView 与 UITableViewController实例详解的详细讲解,希望对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:iOS UITableView 与 UITableViewController实例详解 - Python技术站