To explain the various methods of setting up the top blank of UITableView cell in IOS development _ios

Source: Internet
Author: User


I know no one will voluntarily set this thing, but you must have encountered this problem, the following summary of what may be:



  1, self.automaticallyadjustsscrollviewinsets = NO;



This should be the most common and not easily found cause of the fact that IOS7 has added automaticallyadjustsscrollviewinsets this attribute in Conttoller, when set to Yes (the default yes), If there is only one uiscrollview in the view, or its subclass view, it automatically sets the corresponding inner margin, which allows the scroll to occupy the entire view without the navigation bar being obscured.



Ps:ios7 inside the layout of a lot of problems, the use of AutoLayout will encounter a lot, probably because iOS7 new join AutoLayout still immature caused it.



  2,navigationbar Setup Issues



Although on the surface there is a blank at the top of the tableview, it may actually be caused by a navigationbar setup problem.



Self.navigationController.navigationBar.translucent = NO; After this property is set to No, the TableView will leave a 64.f position on the top to Navigationbar, and there is a small probability that this problem can occur.



3,tableview section Header Height setting problem



This should be the novice encountered more. The reason is the logic of the wonderful iOS, if you set the header (or footer) height is 0, the system will think you do not set, and then 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 setup issues



Looks like 3, doesn't it? Don't you see the difference? Then read it again. This time it was caused by the header view of the TableView, not the header height of the section.



For TableView, not only each section has header,tableview the whole also has header and Footer,api 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 and footer are more harmonious than the section header, as long as you don't touch it, it's okay, but if you touch ... Hum, hum ... is basically set to a 40.f high spacing. Any line of code that appears as follows 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]; 


Yes, you're not wrong, Footerview can not set, footer and header as long as you set any one will make two places are blank. Don't ask me why ...



Of course, what if you really only need one view at some point? Please set the following: (not like silly, build a view of their own, must use disgusting things mody ... )


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 put it bluntly, it has to be set to a very small height, not 0.



About TableView The summary of the top blank is basically the case, if you want to block, it is recommended to write these in Basetableviewcontroller, so you do not have to buckle these things every time. Macro lazy sticky, are common, we should all be able to read. Navigationbar that, because this thing is generally not set here, writing in base is not a good practice.


//
// 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





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.