iOS UITableView 的 Plain和Grouped样式的区别

官方文档写的非常经典:

Table views can have one of two styles, UITableViewStylePlain and UITableViewStyleGrouped. When you create a UITableView instance you must specify a table style, and this style cannot be changed.

In the plain style, section headers and footers float above the content if the part of a complete section is visible. A table view can have an index that appears as a bar on the right hand side of the table (for example, "A" through "Z"). You can touch a particular label to jump to the target section.

The grouped style of table view provides a default background color and a default background view for all cells. The background view provides a visual grouping for all cells in a particular section. For example, one group could be a person's name and title, another group for phone numbers that the person uses, and another group for email accounts and so on. See the Settings application for examples of grouped tables. Table views in the grouped style cannot have an index.

意思是:
tableView有两种style,UITableViewStylePlain和UITableViewStyleGrouped。当你创建一个UITableView实例必须指定其的style,并且这种style是不能被改变的。

Plain style的UITableView

在plain style的tableView中,当一个section的rows有一部分可见时,section的header和footer浮动在内容顶部。plain style的tableView可以有一个section索引,作为一个bar在table的右边(例如A ~ Z)。你可以点击一个特定的标签,跳转到目标section。例如下图:


iOS UITableView 的 Plain和Grouped样式的区别_第1张图片
Group style的UITableView

在grouped style的tableView中,所有单元格拥有一个默认的背景颜色和默认背景视图。背景视图为特定section中的所有cell提供可视分组。例如,一个group可以是一个人的名字和标题,另一个group可以是电话,电子邮件帐户等。可参考iphone“设置”程序。
注意:在grouped style表中不能有一个(右边的)索引。如下图:

iOS UITableView 的 Plain和Grouped样式的区别_第2张图片

Group类型默认设置tableView灰色背景色,cell为白色背景色,section外边缘设置浅灰色边框,cell设置浅灰色间隔线。如下图:


iOS UITableView 的 Plain和Grouped样式的区别_第3张图片

区别总结:

一、UITableViewStylePlain

1.plain类型有多段时,段头停留(自带效果)
2.plain类型默认section之间没有中间的间距和头部间距(想让plain类型的section之间留有空白,需要在UITableView代理方法中return自定义的header和footer,并在自定义的UITableViewHeaderFooterView里面重写setFrame方法)

  1. 扩展:让plain类型的UITableView段头不停留(取消粘性效果)
(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 30;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
二、UITableViewStyleGroup
注意:去掉Group类型的表section头部和中间间隔的方法:

1.设置标题tableHeaderView的高度为特小值,但不能为零,若为零的话,ios会取默认值18,就无法消除头部间距了。

      UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)];
      view.backgroundColor = [UIColor redColor];
      self.tableView.tableHeaderView = view;

2.设置代理方法(中间的留白其实是段尾的高度,代理的作用设置段尾的高度,返回值也不能为0,否则系统启用默认值18)

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
      return 0.01f;
}
//特殊的处理方法也能实现该效果
self.tableView.contentInset = UIEdgeInsetsMake(-44, 0, 0, 0);

3.自定义类继承UITableViewHeaderFooterView,重写setFrame方法,在UITableView代理方法中return 自定义类创建的section的header和footer。

-(void)setFrame:(CGRect)frame{
frame.size.height+=10;
[super setFrame:frame];
}

注意:sectionHeaderHeight/sectionFooterHeight这2个属性只在Grouped类型,且未实现代理方法tableView:heightForHeaderInSection: 时有效,在plain风格下设置无效。故在使用UITableView过程中尽量使用代理方法设置sectionHeader和sectionFooter的高度。

注: 部分转载http://www.jianshu.com/p/3a5063993368

你可能感兴趣的:(iOS UITableView 的 Plain和Grouped样式的区别)