【iOS】UITableview cell 頂部空白的n種設定方法,iosuitableview

來源:互聯網
上載者:User

【iOS】UITableview cell 頂部空白的n種設定方法,iosuitableview

  我知道沒人會主動設定這個東西,但是大家一定都遇到過這個問題,下面總結下可能是哪些情況:

 

  1, self.automaticallyAdjustsScrollViewInsets = NO; 

  這個應該是最常見而且不容易被發現的原因,起因是iOS7在Conttoller中新增了automaticallyAdjustsScrollViewInsets這個屬性,當設定為YES時(預設YES),如果視圖裡面存在唯一一個UIScrollView或其子類View,那麼它會自動化佈建相應的內邊距,這樣可以讓scroll佔據整個視圖,又不會讓導覽列遮蓋。

  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的話,系統會認為你沒設定,然後將其設定為40.f。所以需要將其設定為一個較小的數:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {    return 0.001f;}
   4,tableview的header、footer設定問題

  和3很像是不是?沒發現區別嗎?那就再讀一次看看。這次是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高的間距。出現如下任意一行代碼均會引起這個問題:

   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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.