Elaborate on the various Settings for UITableview cell top white space in iOS development

  • 2020-05-30 21:08:21
  • OfStack

I know no one is going to set this up on their own initiative, but I'm sure you've all had this problem before. Here's a summary of what it might be like:

1, self.automaticallyAdjustsScrollViewInsets = NO;

This should be the most common and is not easy to find the reason, cause is iOS7 in Conttoller automaticallyAdjustsScrollViewInsets this attribute has been added, when set to YES (default YES), if there are only 11 UIScrollView view or its subclasses View, then it will automatically set the appropriate padding, so that we can make scroll occupy the whole view, cover again, won't make the navigation bar.

There are a lot of layout problems in PS: iOS7. When using autolayout, I will encounter a lot of problems, probably because iOS7 is still immature when adding autolayout.

2. navigationbar setting problem

Although it appears that there is a white space at the top of tableview, it may actually be due to a problem with the navigationbar setup.

self. navigationController. navigationBar. translucent = NO; When this property is set to no, tableview will leave a space of 64.f at the top for navigationbar. There is also a small probability of causing this problem.

3, tableview section header height setting problem

This should be the novice encountered more. If you set header (or footer) to a height of 0, the system will assume that you have not set it, and will set it to 40.f. So you need to set it to a smaller number:


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

4, tableview header, footer Settings problem

It's very similar to 3, right? No difference? Read it one more time. This time it is the header view of tableview, not the header height of section.

For tableview, not only each section has header, but tableview as a whole also has header and footer. API is as follows:


@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 

This header is more compatible with footer than section's header, as long as you don't touch it, but if you do... Hum, hum... It will basically be set with a high spacing of 40.f. Any of the following lines of code can cause this problem:


  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]; 

Yes, you're right, footerView can't be set, footer and header can be set to any one of them and you'll have blank Spaces in both places. Don't ask me why...

Of course, what if sometimes all you really need is one view? Please set it up as follows :(it seems silly to build an view, you have to use something gross...)


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

To be clear, you still have to set it to a very small height, not a zero.

So much for the summary of the white space at the top of tableView. If you want to mask it, it is recommended to write it in baseTableViewController so that you don't have to button it every time. Macro too lazy to stick, are common, you should be able to understand. navigationbar that, because this thing 1 is not set here, it's not a good idea to write it in base.


//
// HLNBaseTableViewController.m
// HLN-IMDemo
//
// Created by heiline on 15/8/25.
// Copyright (c) 2015 years  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


Related articles: