IOS Tableview的两种系统样式

默认情况下,如果不带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的两种系统样式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值