詳解iOS開發(fā)中UITableview cell 頂部空白的多種設置方法
我知道沒人會主動設置這個東西,但是大家一定都遇到過這個問題,下面總結下可能是哪些情況:
1, self.automaticallyAdjustsScrollViewInsets = NO;
這個應該是最常見而且不容易被發(fā)現(xiàn)的原因,起因是iOS7在Conttoller中新增了automaticallyAdjustsScrollViewInsets這個屬性,當設置為YES時(默認YES),如果視圖里面存在唯一一個UIScrollView或其子類View,那么它會自動設置相應的內邊距,這樣可以讓scroll占據(jù)整個視圖,又不會讓導航欄遮蓋。
PS:iOS7里面的布局問題挺多的,使用autolayout的時候會遇到好多,大概是因為iOS7新加入autolayout還還不成熟導致的吧。
2,navigationbar設置問題
雖然表面上看是tableview頂部有空白,但實際上可能是因為navigationbar設置問題導致。
self.navigationController.navigationBar.translucent = NO; 這個屬性設為no之后,tableview會在上方留出64.f的位置給navigationbar,也有小概率導致這個問題。
3,tableview section header高度設置問題
這個應該是新手遇到的比較多的。起因是iOS奇葩的邏輯,如果你設置header(或者footer)高度是0的話,系統(tǒng)會認為你沒設置,然后將其設置為40.f。所以需要將其設置為一個較小的數(shù):
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.001f; }
4,tableview的header、footer設置問題
和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要和諧一些,只要你不去主動碰它就沒事,但是如果你碰了...哼,哼...基本上會被設置出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也不能設置,footer和header只要設置了任意一個都會使兩個地方都出現(xiàn)空白。不要問我為什么...
當然,如果有的時候真的只需要其中一個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
相關文章
iOS開發(fā)中UIDatePicker控件的使用方法簡介
這篇文章主要介紹了iOS開發(fā)中UIDatePicker控件的使用方法簡介,用來處理各種時間日期的選擇,需要的朋友可以參考下2015-11-11iOS 11更新后及iPhone X推出后工程中遇到的問題及適配方法
這篇文章主要介紹了iOS 11更新后及iPhone X推出后工程中遇到的問題及適配,需要的朋友可以參考下2017-10-10iOS開發(fā)之tableView cell的展開收回功能實現(xiàn)代碼
本文介紹了iOS開發(fā)之tableView cell的展開收回功能實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01實例講解iOS應用開發(fā)中使用UITableView創(chuàng)建自定義表格
這篇文章主要介紹了iOS應用開發(fā)中使用UITableView創(chuàng)建自定義表格的方法,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01解析iOS應用開發(fā)中對設計模式中的抽象工廠模式的實現(xiàn)
這篇文章主要介紹了解析iOS應用開發(fā)中對設計模式中的抽象工廠模式的實現(xiàn),示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03