你好,歡迎來到IOS教程網

 Ios教程網 >> IOS教程 >> 關於IOS教程 >> 詳解iOS開發中UITableview cell 頂部空白的多種設置方法

詳解iOS開發中UITableview cell 頂部空白的多種設置方法

編輯:關於IOS教程

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

  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
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved