[IOS] n blank setting methods on the top of UITableview cell, iosuitableview

Source: Internet
Author: User

[IOS] n blank setting methods on the top of UITableview cell, iosuitableview

I know that no one will take the initiative to set this item, but everyone must have encountered this problem. Below we will summarize the possible situations:

 

1, self. automaticallyAdjustsScrollViewInsets = NO;

This should be the most common cause and is not easy to find. The reason is that iOS7 adds the automaticallyAdjustsScrollViewInsets attribute in Conttoller. When it is set to YES (YES by default ), if a View contains a unique UIScrollView or its subclass View, it automatically sets the corresponding padding so that scroll can occupy the entire View without overwriting the navigation bar.

PS: There are many layout problems in iOS7. There will be many problems when using autolayout, probably because iOS7's new autolayout is still immature.

 

2. navigationbar settings

Although there is a blank area on the top of tableview, it may be caused by a problem in navigationbar settings.

Self. navigationController. navigationBar. translucent = NO; after this attribute is set to no, tableview will leave a 64. f position on the top of the page to navigationbar, which may be caused by a small probability.

 

3. Question about tableview section header height settings

This should be a lot of new users. The reason is that the logic of iOS is amazing. If you set the header (or footer) height to 0, the system will think that you have not set it, and then set it to 40.f. Therefore, you need to set it to a smaller number:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {    return 0.001f;}
4. tableview header and footer settings

Like 3, isn't it? No difference found? Then read it again. This is caused by the header view of tableview, rather than the header height of section.

For tableview, not only each section has a header, but tableview also has a header and footer. The 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 and footer should be more harmonious than the section header. As long as you don't touch it, it will be okay, but if you touch it... hum, hum... basically, 40 is set. f. Any line of code below will 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];

No, you didn't think it was wrong. footerView cannot be set either. If footer and header are set to any one, both spaces will appear. Don't ask me why...

Of course, what should I do if I only need one view? Set as follows:(It seems silly to build a view by yourself. Do you have to use disgusting things ...)  

    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, you have to set it to a very small height, instead of 0.

 

The summary of the blank spaces at the top of tableView is basically the same. If you want to block them, we recommend that you write them in baseTableViewController so that you don't need to deduct them every time. Macros are too lazy to stick. They are common and can be understood by everyone. Navigationbar, because this is generally not set here, writing in the 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: _ tableV IewStyle]; [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 succeeded :( NSIndexPath *) indexPath {return 40.f;}-(CGFloat) tableView :( UITableView *) tableView succeeded :( 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.