詳解iOS開發(fā)中UITableview cell 頂部空白的多種設(shè)置方法
我知道沒人會主動設(shè)置這個東西,但是大家一定都遇到過這個問題,下面總結(jié)下可能是哪些情況:
1, self.automaticallyAdjustsScrollViewInsets = NO;
這個應(yīng)該是最常見而且不容易被發(fā)現(xiàn)的原因,起因是iOS7在Conttoller中新增了automaticallyAdjustsScrollViewInsets這個屬性,當(dāng)設(shè)置為YES時(默認YES),如果視圖里面存在唯一一個UIScrollView或其子類View,那么它會自動設(shè)置相應(yīng)的內(nèi)邊距,這樣可以讓scroll占據(jù)整個視圖,又不會讓導(dǎo)航欄遮蓋。
PS:iOS7里面的布局問題挺多的,使用autolayout的時候會遇到好多,大概是因為iOS7新加入autolayout還還不成熟導(dǎo)致的吧。
2,navigationbar設(shè)置問題
雖然表面上看是tableview頂部有空白,但實際上可能是因為navigationbar設(shè)置問題導(dǎo)致。
self.navigationController.navigationBar.translucent = NO; 這個屬性設(shè)為no之后,tableview會在上方留出64.f的位置給navigationbar,也有小概率導(dǎo)致這個問題。
3,tableview section header高度設(shè)置問題
這個應(yīng)該是新手遇到的比較多的。起因是iOS奇葩的邏輯,如果你設(shè)置header(或者footer)高度是0的話,系統(tǒng)會認為你沒設(shè)置,然后將其設(shè)置為40.f。所以需要將其設(shè)置為一個較小的數(shù):
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.001f;
}
4,tableview的header、footer設(shè)置問題
和3很像是不是?沒發(fā)現(xiàn)區(qū)別嗎?那就再讀一次看看。這次是tableview的header視圖引起的,而不是section的header高度引起。
對于tableview,不僅每個section有header,tableview整體也有header和footer,API如下:
@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要和諧一些,只要你不去主動碰它就沒事,但是如果你碰了...哼,哼...基本上會被設(shè)置出40.f高的間距。出現(xiàn)如下任意一行代碼均會引起這個問題:
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也不能設(shè)置,footer和header只要設(shè)置了任意一個都會使兩個地方都出現(xiàn)空白。不要問我為什么...
當(dāng)然,如果有的時候真的只需要其中一個view的話該怎么辦呢?請如下設(shè)置:(似不似傻,自己建一個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)];
說白了,還是得設(shè)置成一個很小的高度,而不是0才行。
關(guān)于tableView頂部空白的總結(jié)基本就這些了,如果想屏蔽的話,建議把這些寫在baseTableViewController里面,這樣就不用每次都扣這些東西了。宏懶得粘了,都是常見的,大家應(yīng)該都能看懂。navigationbar那個,因為這個東西一般不在這里設(shè)置,寫在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
相關(guān)文章
IOS倒計時設(shè)置UIButton標(biāo)題title的抖動問題
這篇文章主要介紹了IOS倒計時設(shè)置UIButton標(biāo)題title的抖動問題,在發(fā)送驗證碼后,button狀態(tài)需要變?yōu)閐isable,每隔一秒顯示倒計時時間,下面通過本文給大家介紹設(shè)置方法,一起看看吧2016-12-12
iOS中滑動控制屏幕亮度和系統(tǒng)音量(附加AVAudioPlayer基本用法和Masonry簡單使用)
這篇文章主要介紹了iOS中滑動控制屏幕亮度和系統(tǒng)音量(附加AVAudioPlayer基本用法和Masonry簡單使用)的相關(guān)資料,需要的朋友可以參考下2016-12-12
iOS中tableView cell分割線的一些設(shè)置技巧
在項目開發(fā)中我們會常常遇到tableView 的cell分割線顯示不全,左邊會空出一截像素,更有甚者想改變系統(tǒng)的分割線,下面通過這篇文章來一起學(xué)習(xí)學(xué)習(xí)在iOS中tableView cell分割線的一些設(shè)置技巧,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05
ios基于UITableViewController實現(xiàn)列表
這篇文章主要介紹了ios基于UITableViewController實現(xiàn)列表的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01

