IOS Tableview的两种系统样式

来源:互联网 发布:淘宝创业培训班 编辑:程序博客网 时间:2024/04/26 04:17

默认情况下,如果不带xib。tableview样式是plain样式,不自动产生组头和组尾。如下图,创建一个不带xib的Controller
这里写图片描述
看这段代码:

- (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"];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return 2;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return 5;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    //两个参数的方法  表示 从cell空闲队列中去名字叫@“cell”的单元格,如果没有取到,tableView会根据我们之前注册样式,帮我们创建一个cell使用,如果取到,就使用取出来的    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];    cell.textLabel.text = @"这是一个单元格";    return cell;}

实现效果如下: 两个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"];}

效果如下:
这里写图片描述
自动产生分组头,和分组脚。好了,这就是tableview的两种系统样式