2016-07-20 5 views
2

特定の条件を満たす場合は、表のビューヘッダー(セクションヘッダーではありません)を表示するUITableViewControllerがあります。ロード時にヘッダービューの有無にかかわらずコントローラーを表示するだけで問題ありません。しかし、ヘッダーを表示してから削除すると、ヘッダーのフレームが配置されていた間隔が毎回テーブルビューの一番上にとどまります。UITableView - ビューが削除された場合に表の見出しの間隔が表示されます。

運とテーブルビューのヘッダを削除するとき、私は、次のすべての組み合わせを試してみました:他に

[self.tableView beginUpdates]; 
self.tableView.tableHeaderView = nil; 
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero]; 
[self.tableView reloadData]; 
[self.tableView endUpdates]; 

誰もがこのに遭遇しましたか?

表ビューのヘッダーコード:

- (void)updateTableHeaderView 
{ 
    if (self.alerts.count && self.isViewLoaded) { 
     [self.tableView beginUpdates]; 

     UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.bounds), 40.0f)]; 
     headerView.preservesSuperviewLayoutMargins = YES; 
     self.tableView.tableHeaderView = headerView; 

     UIButton *alertButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     alertButton.tintColor = [UIColor whiteColor]; 
     alertButton.translatesAutoresizingMaskIntoConstraints = NO; 
     [alertButton setBackgroundImage:[UIImage imageWithColor:alertTintColor] forState:UIControlStateNormal]; 
     [alertButton setBackgroundImage:[UIImage imageWithColor:alertHighlightedTintColor] forState:UIControlStateHighlighted]; 
     [alertButton addTarget:self action:@selector(alertButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [headerView addSubview:alertButton]; 

     NSDictionary *views = NSDictionaryOfVariableBindings(alertButton); 
     NSDictionary *metrics = nil; 

     [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[alertButton]|" options:0 metrics:metrics views:views]]; 
     [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[alertButton]|" options:0 metrics:metrics views:views]]; 

     [self.tableView endUpdates]; 
    } 
    else if (self.isViewLoaded) { 
     [self.tableView beginUpdates]; 
     self.tableView.tableHeaderView = nil; 
     [self.tableView endUpdates]; 
    } 
} 
+0

はうまくいきます!!どうぞよろしくお願いしますか? – NSSakly

+0

@ NSSakly必要に応じてテーブルビューヘッダーを生成(および削除)するコードを追加しました。問題は、ヘッダーを表示して(ユーザーがアラートを受け取ったときに)ヘッダーを表示すると、ヘッダーを削除したいということです。しかし、それを取り除くと、ヘッダーがあるスペース(つまり、テーブルの上端と最初のセクション/行の間の隙間)が残されます。 – JimmyJammed

答えて

4

これはiOSのバグで表示され、これは除去するために、現在の回避策ですtableHeaderViewと固定されるまでの間隔:

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.bounds), 1.0f)]; 
+0

ありがとう!あなたは私の日を救う! – Arsen

+0

あなたも私の一日を保存しました:) –

+1

これは幅が0.0fに設定されている場合にも機能します –

-1

あなたにもそれを扱うかどうかを確認してください:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
+0

OPで述べたように、この問題はセクションヘッダーではなくテーブルビューヘッダーにあります(self.tableView.tableHeaderViewなど)。 heightForHeaderInSectionプロトコルメソッドは、セクションヘッダーにのみ適用されます。 – JimmyJammed

関連する問題