美文网首页
iOS UITableView 的 Plain和Grouped样

iOS UITableView 的 Plain和Grouped样

作者: 魔力双鱼 | 来源:发表于2019-01-03 14:46 被阅读0次

    区别总结:

    一、UITableViewStylePlain

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

    3.扩展:让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

    默认会有headview和footview,头尾会空出一些距离.

    注意:去掉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的高度。

    //让cell不能选中
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //也可以让分割线消失
    _TableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    相关文章

      网友评论

          本文标题:iOS UITableView 的 Plain和Grouped样

          本文链接:https://www.haomeiwen.com/subject/djkplqtx.html