详解iOS开发中UITableview cell 顶部空白的多种设置方法

详解iOS开发中UITableview cell 顶部空白的多种设置方法,第1张

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

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

  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,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,kScreenSize.wIDth,0.0001f)];self.tableVIEw.tableFooterVIEw = [[UIVIEw alloc] initWithFrame:CGRectMake(0,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.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

总结

以上是内存溢出为你收集整理的详解iOS开发中UITableview cell 顶部空白的多种设置方法全部内容,希望文章能够帮你解决详解iOS开发中UITableview cell 顶部空白的多种设置方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149440.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存