您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

UITableView 頂部莫名空白的四種成因與解法

編輯:iOS 相關教學

這個問題沒人會刻意去設定,但幾乎每個人都遇到過:明明沒寫錯,列表上方卻多出一塊空白。以下整理最常見的四個原因。

原因一:automaticallyAdjustsScrollViewInsets

self.automaticallyAdjustsScrollViewInsets = NO;

這是最常見也最不容易察覺的元兇。iOS 7 在 UIViewController 新增了這個屬性,預設為 YES;當視圖裡只有一個 UIScrollView(或其子類)時,系統會自動幫它加上內距,讓內容不被導覽列遮住。若你的版面已經自行處理過間距,就會變成「多加了一次」。

原因二:navigationBar 的 translucent 設定

表面上看是 tableview 上方有空白,實際上可能來自導覽列的布局方式。

self.navigationController.navigationBar.translucent = NO;

設為 NO 之後,tableview 會在上方保留大約 64pt 的空間給導覽列,某些情境下也會造成類似現象。

原因三:section header 高度設為 0

新手最常踩的一個坑。系統對 section header 有個特殊邏輯:如果你回傳 0,它會認為「你沒有設定」,於是自動套用預設的約 40pt。解法是回傳一個極小值:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001f;
}

原因四:tableHeaderView/tableFooterView

仔看板,這跟第三點不一樣:這次是 tableview 整體的 header/footer 視圖,而不是 section 的。API 如下:

@property (nonatomic, strong, nullable) UIView *tableHeaderView;
@property (nonatomic, strong, nullable) UIView *tableFooterView;

這兩個屬性只要你「碰了」就可能出事。以下任何一行都會導致空白出現:

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];

而且只要設其中一個,兩邊都會出現空白。解法同樣是給一個極小的高度而不是 0:

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)];

建議:把這些處理收進 Base 控制器

與其每頁都踩一次,不如寫在 BaseTableViewController 裡一次解決。導覽列相關設定因為通常不在此層處理,不建議一併放進去。

#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];

    // 用極小高度取代 0,避開系統的預設值邏輯
    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 - UITableViewDataSource / Delegate
-(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