码神之路:Object-C篇

OC UI总结之--tableview相关用法和问题

2016-12-19  本文已影响327人  一只特立独行的道哥

1. 去除某个tableviewCell的分割线

有时候部分tableviewCell不需要分割线,有些需要,这个时候可以给指定类的cell

-(void)addSubview:(UIView *)view{
    if (![view isKindOfClass:[NSClassFromString(@"_UITableViewCellSeparatorView") class]] && view) {
        [super addSubview:view];
    }
}

2. viewForHeaderInSection 的section从1开始而不是从0开始

使用 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 时发现section是从1开始而不是从0,最后查看API的时候发现原文中说道:

This method only works correctly when tableView:heightForHeaderInSection: is also implemented.这个方法只有当tableView:heightForHeaderInSection:实现的时候才能正确执行。

好吧,确实是一个天坑,谨记了。。。。

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

就好了

3. tableView设置UITableViewStyleGrouped顶部有空余高度

tableView 有两种UITableViewStyle :

  • UITableViewStylePlain 普通(默认值)
  • UITableViewStyleGrouped 分组
    默认情况下plain时第一行cell的上边界是和导航条(屏幕顶端)紧贴着的。如果你改成了grouped的话,就会有一个默认的间隙,这个间隙是scrollView(tableview也是一种)在导航栏下的自动缩进---个人理解,如果不对请指正。

我尝试过如下方法:

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return  0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

最终还是用设置contentInset收拾了

self.tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);

目前还没弄明白为啥会这样,先放在这里做个笔记,以后好用。。。。,如果哪位知道请告知我一下,多谢。。。

上一篇 下一篇

猜你喜欢

热点阅读