服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - 详解iOS开发中UITableview cell 顶部空白的多种设置方法

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

2021-01-16 14:51heiline IOS

这篇文章主要介绍了详解iOS开发中UITableview cell 顶部空白的多种设置方法的相关资料,需要的朋友可以参考下

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

  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。所以需要将其设置为一个较小的数:

?
1
2
3
- (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如下:

?
1
2
@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高的间距。出现如下任意一行代码均会引起这个问题:

?
1
2
3
4
5
6
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呗,非得用着恶心的东西么...)  

?
1
2
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里面不是一个好的做法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// 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

延伸 · 阅读

精彩推荐
  • IOSiOS常见的几个修饰词深入讲解

    iOS常见的几个修饰词深入讲解

    这篇文章主要给大家介绍了关于iOS常见的几个修饰词的相关资料,iOS修饰词包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通过示...

    郡王丶千夜7422021-05-10
  • IOSIOS网络请求之AFNetWorking 3.x 使用详情

    IOS网络请求之AFNetWorking 3.x 使用详情

    本篇文章主要介绍了IOS网络请求之AFNetWorking 3.x 使用详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    总李写代码6892021-03-04
  • IOSiOS逆向教程之logify跟踪方法的调用

    iOS逆向教程之logify跟踪方法的调用

    这篇文章主要给大家介绍了关于iOS逆向教程之logify跟踪方法调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    Mr.Guo11472021-04-28
  • IOSxcode8提交ipa失败无法构建版本问题的解决方案

    xcode8提交ipa失败无法构建版本问题的解决方案

    xcode升级到xcode8后发现构建不了新的版本。怎么解决呢?下面小编给大家带来了xcode8提交ipa失败无法构建版本问题的解决方案,非常不错,一起看看吧...

    Cinna丶7542021-02-03
  • IOS谈一谈iOS单例模式

    谈一谈iOS单例模式

    这篇文章主要和大家谈一谈iOS中的单例模式,单例模式是一种常用的软件设计模式,想要深入了解iOS单例模式的朋友可以参考一下...

    彭盛凇11872021-01-19
  • IOSiOS中时间与时间戳的相互转化实例代码

    iOS中时间与时间戳的相互转化实例代码

    这篇文章主要介绍了iOS中时间与时间戳的相互转化实例代码,非常具有实用价值,需要的朋友可以参考下。...

    张无忌!4812021-03-09
  • IOSiOS APP实现微信H5支付示例总结

    iOS APP实现微信H5支付示例总结

    这篇文章主要介绍了iOS APP实现微信H5支付示例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    一张小A11332021-06-01
  • IOSiOS10 Xcode8适配7个常见问题汇总

    iOS10 Xcode8适配7个常见问题汇总

    这篇文章主要为大家详细汇总了iOS10 Xcode8适配7个常见问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    索马里猫10332021-02-01