国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

詳解iOS開發(fā)中UITableview cell 頂部空白的多種設(shè)置方法

2020-07-26 03:22:18
字體:
供稿:網(wǎng)友

我知道沒人會主動設(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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 呼和浩特市| 铁岭市| 神池县| 夏邑县| 寿阳县| 丰都县| 连江县| 白银市| 城口县| 巴中市| 加查县| 安义县| 泗洪县| 静安区| 永新县| 平江县| 莱芜市| 财经| 宜君县| 潮安县| 庄河市| 阳西县| 二连浩特市| 波密县| 万荣县| 汤原县| 泾川县| 吉林省| 邵武市| 潜山县| 名山县| 梁山县| 将乐县| 海林市| 铁力市| 东源县| 喜德县| 岐山县| 芒康县| 大城县| 浠水县|