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

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也是一种)在导航栏下的自动缩进---个人理解,如果不对请指正。

我尝试过如下方法:

  • 设置一个空的tableviewheader: 不行
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
  • 设置第一个section的sectionHeader/FooterHeight 为0.01(iOS中设置为0相当于没设置!)也不行

-(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);

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容