iOS TableView 的两种系统样式
默认情况下,如果不带 XIB,TableView 样式为 plain,不自动产生组头和组尾。创建一个不带 XIB 的 Controller 时,即使设置多个 Section,也不会显示分组效果。
- (void)viewDidLoad {
[super viewDidLoad];
// 没有 XIB 的 TableViewController 默认为 plain 样式,分 Section 也不会有组头、组尾
// 如果加上下面代码,就会有组头组尾
// self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStyleGrouped];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = @"这是一个单元格";
return cell;
}
此时两个 Section 也没有分组效果。
如果将代码强写为分组样式:
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStyleGrouped];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
此时会自动产生分组头和分组脚。
以上即为 TableView 的两种系统样式区别。


