【iOS】UITableview cell 顶部空白的n种设置方法

  我知道没人会主动设置这个东西,但是大家一定都遇到过这个问题,下面总结下可能是哪些情况:

  1, self.automaticallyAdjustsScrollViewInsets = NO;

  这个应该是最常见而且不容易被发现的原因,起因是iOS7在Conttoller中新增了automaticallyAdjustsScrollViewInsets这个属性,当设置为YES时(默认YES),如果视图里面存在唯一一个UIScrollView或其子类View,那么它会自动设置相应的内边距,这样可以让scroll占据整个视图,又不会让导航栏遮盖。

  PS:iOS7里面的布局问题挺多的,使用autolayout的时候会遇到好多,大概是因为iOS7新加入autolayout还还不成熟导致的吧。

  2,navigationbar设置问题

  虽然表面上看是tableview顶部有空白,但实际上可能是因为navigationbar设置问题导致。

   self.navigationController.navigationBar.translucent = NO; 这个属性设为no之后,tableview会在上方留出64.f的位置给navigationbar,也有小概率导致这个问题。

  3,tableview section header高度设置问题

  这个应该是新手遇到的比较多的。起因是iOS奇葩的逻辑,如果你设置header(或者footer)高度是0的话,系统会认为你没设置,然后将其设置为40.f。所以需要将其设置为一个较小的数:

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

  4,tableview的header、footer设置问题

  和3很像是不是?没发现区别吗?那就再读一次看看。这次是tableview的header视图引起的,而不是section的header高度引起。

  对于tableview,不仅每个section有header,tableview整体也有header和footer,API如下:

@property (nonatomic, strong, nullable) UIView *tableHeaderView;                           // accessory view for above row content. default is nil. not to be confused with section header
@property (nonatomic, strong, nullable) UIView *tableFooterView;                           // accessory view below content. default is nil. not to be confused with section footer

  这个header和footer要比section的header要和谐一些,只要你不去主动碰它就没事,但是如果你碰了...哼,哼...基本上会被设置出40.f高的间距。出现如下任意一行代码均会引起这个问题:

   self.tableView.tableHeaderView = nil;

   self.tableView.tableHeaderView = [[UIView alloc] init];

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

   self.tableView.tableFooterView = nil;

   self.tableView.tableFooterView = [[UIView alloc] init];

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

  对,你没想错,footerView也不能设置,footer和header只要设置了任意一个都会使两个地方都出现空白。不要问我为什么...

  当然,如果有的时候真的只需要其中一个view的话该怎么办呢?请如下设置:(似不似傻,自己建一个view呗,非得用着恶心的东西么...)  

    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 0.0001f)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 0.0001f)];

  说白了,还是得设置成一个很小的高度,而不是0才行。

  关于tableView顶部空白的总结基本就这些了,如果想屏蔽的话,建议把这些写在baseTableViewController里面,这样就不用每次都扣这些东西了。宏懒得粘了,都是常见的,大家应该都能看懂。navigationbar那个,因为这个东西一般不在这里设置,写在base里面不是一个好的做法。

//
//  HLNBaseTableViewController.m
//  HLN-IMDemo
//
//  Created by heiline on 15/8/25.
//  Copyright (c) 2015年 baidu. All rights reserved.
//

#import "HLNBaseTableViewController.h"

@interface HLNBaseTableViewController () 

@end

@implementation HLNBaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView = [[UITableView alloc] initWithFrame:(CGRect){CGPointZero, kScreenSize} style:_tableViewStyle];
    [self.view addSubview:self.tableView];

    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 0.0001f)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 0.0001f)];

    if (self.navigationController != nil) {
        self.tableView.height -= kNavBarH + kStatusBarH;
    }

    if (self.tabBarController != nil) {
        if (self.navigationController.childViewControllers.count == 1) {
            self.tableView.height -= kTabBarH;
        }
    }
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)dealloc {
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
}

#pragma mark Table View Data Source And delegate Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[UITableViewCell alloc] init];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40.f;
}

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

@end
时间: 2024-08-05 18:52:22

【iOS】UITableview cell 顶部空白的n种设置方法的相关文章

iOS Mac忘记登录密码的4种解决方法

4种方法: 一.使用Apple ID重置用户账户密码 使用这个方法有一个前提 如上图红框,此项必须勾选,否则无法使用Apple ID重置密码.(如果你不记得有没有勾选,则你起码要记得首次启动 OS X 或装有 OS X Lion 或更高版本的全新 Mac 时,你会进入“OS X 设置助理”,其中需要你输入 Apple ID.输入 Apple ID 后,请在“创建您的帐户”阶段选择“允许我的 Apple ID 重设此用户的密码”选项.) 好了,来说下具体实现的方法: 如果您在登录窗口错误输入帐户密

【iOS开发-30】UITabBarController的几种代理方法以及结合NSUserDefaults还原上次退出时被选中视图控制器和视图控制器的顺序

一.UITabBarController的几种代理方法 在AppDelegate.h中加入一个协议<UITabBarControllerDelegate>,然后再AppDelegate.m中添加如下代理方法: 如果没有效果,可能是缺少tbCon1.delegate=self;把代理指定给当前AppDelegate. -(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:

宝塔Linux nginx http强制跳转https几种设置方法

最近很多人都比较关注SSL证书的申请与设置,春哥技术博客近期也为大家介绍了国内一些免费SSL证书申请流程及比较常用的VPS建站环境包如何安装配置SSL证书.希望有需要的朋友能成功为自己的站点安装SSL证书.在这里春哥为大家分享几种Nginx环境下强制http转https设置方法. 一.请先找准Nginx配置文件的位置: 其实配置文件位置不是很难找,一般是在/nginx/conf/vhost/目录下,文件命名一般是"你的域名.conf".下面介绍几种常用VPS控制面板Nginx配置文件的

远程管理设备telnet的N种设置方法【你确定都会吗】

作为一名网络工程师,日常管理和配置设备,可以选择的方法有很多,很多(如下图), 但是最常用的配置手段,任然是本地使用console口,远程使用telnet(或SSH),本文以思科设备演示几种telnet的设置方法,抛砖引玉,供大家参考. 基本配置: R1(config)#inter fa0/0 R1(config-if)#ip add 10.1.1.1 255.255.255.0 R1(config-if)#no shutdown R2(config)#inter fa0/0 R2(config

UITableView 去除 顶部空白

NO1:代码方式 //self.automaticallyAdjustsScrollViewInsets = NO; NO2:storyboard方式 来自为知笔记(Wiz)

ios基础之关闭输入框的几种常见方法集锦

第一种: 改写ruturn方法,点击键盘的return键实现关闭键盘(注意field1的代理是自己以及前面一定要有代理) 这种方法是最简单的也是最常用的方法! 未完,其他方法随后奉上....

iOS学习之UISwitch控件两种使用方法和监听

一.第一种创建UISwitch控件的方法,在代码中动态创建. 1.打开Xcode  4.3.2, 新建项目Switch,选择Single View Application. 2.打开ViewController.m文件在viewDidLoad方法里添加代码: - (void)viewDidLoad{ [super viewDidLoad]; UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 2

关于ios 里面碰到内存错误的两种设置

1.EXC_BAD_ACCESS内存错误与NSZombieEnabled EXC_BAD_ACCESS是最常见的错误了,这个一般是访问了释放了的内存地址空间造成的.比如一个对象已经dealloc了,如果你仍向这个对象发 送消息,就会出现这个错误.由于出现这个错误时,几乎不显示什么有用的信息,我们根本无法确定程序错在何处.使用NSZombieEnabled环境变量 可以很好的解决这个问题. 打开你的工程,选择菜单“Product->Edit Scheme”或快捷键“Commend+<” NSZo

Oracle数据OA信用盘平台出租远程连接的四种设置方法和注意事项

OA信用盘平台出租论坛:haozbbs.com Q1446595067 第一种情况: 若oracle服务器装在本机上,那就不多说了,连接只是用户名和密码的问题了.不过要注意环境变量%ORACLE_HOME%/network/admin/是否设置. 第二种情况: 本机未安装oracle服务器,也未安装oracle客户端.但是安装了pl sql development.toad sql development.sql navigator等管理数据库的工具.在虚拟机或者另一台电脑上安装了oracle服