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, 0, kScreenSize.width, 0.0001f)];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 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, 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










