2015-07-30 20 views
6

Chcę wycentrować zawartość mojego UITableView, która zawiera headerView i footerView utworzoną w scenopisie i UITableViewCell s. Jak mogę to osiągnąć?Jak wyrównać (wyśrodkować) zawartość UITableView?

Oto, co próbuję wdrożyć, aby rozwiązać mój problem, ale to nie działa.

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    CGFloat height = self.tableView.frameHeight - self.navigationController.navigationBar.frameHeight - [UIApplication sharedApplication].statusBarFrame.size.height - (self.rowCount * self.rowHeight); 
    self.tableView.tableHeaderView.frameHeight = height/2.0; 
} 

Więc odjąć wysokość navigationBar & StatusBar i komórki wysokość do wysokości tableview, aby uzyskać wysokość pustego obszaru. Teraz, gdy otrzymam wysokość w pustym obszarze, podzielę ją na 2 dla stopki i widoku nagłówka.

+0

zestaw ramek widoku tabeli dynamicznie –

+4

wkleić kod, co próbowałeś? –

+0

@LalitKumar, zaktualizowałem pytanie. Dzięki. – Xchord

Odpowiedz

15

W viewWillAppear aw didRotateFromInterfaceOrientation funkcji:

CGFloat headerHeight = (self.view.frame.size.height - (ROW_HEIGHT * [self.tableView numberOfRowsInSection:0])))/2; 

self.tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, -headerHeight, 0); 

to rozwiąże problem.

EDIT

Wywołanie updateTableViewContentInset funkcję w viewWillLayoutSubviews i po każdym reloadData:

Ojective-C

- (void)updateTableViewContentInset { 
    CGFloat viewHeight = self.view.frame.size.height; 
    CGFloat tableViewContentHeight = self.tableView.contentSize.height; 
    CGFloat marginHeight = (viewHeight - tableViewContentHeight)/2.0; 

    self.tableView.contentInset = UIEdgeInsetsMake(marginHeight, 0, -marginHeight, 0); 
} 

Swift 4

func updateTableViewContentInset() { 
    let viewHeight: CGFloat = view.frame.size.height 
    let tableViewContentHeight: CGFloat = tableView.contentSize.height 
    let marginHeight: CGFloat = (viewHeight - tableViewContentHeight)/2.0 

    self.tableView.contentInset = UIEdgeInsets(top: marginHeight, left: 0, bottom: -marginHeight, right: 0) 
} 
+3

Twoje rozwiązania są bardzo eleganckie i czyste, po prostu nie powiodą się w przypadku, gdy masz dynamiczną liczbę komórek. Jeśli Twoja tabela ma zbyt wiele komórek, nie będzie wyświetlana poprawnie. Tak więc następujący problem rozwiąże ten problem: 'headerHeight = MAX (0, headerHeight);' –

+1

Dlaczego nie używać 'self.tableView.rowHeight' zamiast' ROW_HEIGHT'? –

-1

Set rama Twojego UITableView jak ten

CGFloat width = self.view.frame.size.width; 
CGFloat height = self.view.frame.size.height; 
CGFloat tableHeight = 200.0; //your table height 
self.tableView.frame = CGRectMake(0,(height-tableHeight)/2,width,tableHeight); 

lub spróbować

enter image description here

+1

Dzięki, ale nie chcę zmieniać ramki mojego tableView, ponieważ używam autolayout. – Xchord

+0

zaktualizowałem moją odpowiedź –

0

Dodaj UIView na górze (UIView powinny być rodzicem swojej Tableview) Twojego TableView z ujęć . Następnie ustaw swoje ograniczenia, aby wypełnić widok. Teraz ustaw rozmiar TableView i ustaw jego wiązania, aby wyśrodkować zarówno w pionie, jak iw poziomie górę. To rozwiąże twój problem.

0

Dowiedziałem się, co powoduje, że moje obliczenia są nieprawidłowe. Wartość self.tableView.bounds.size.height różni się od rzeczywistej wysokości w viewWillAppear. Zamiast tego użyłem self.view.bounds.size.height.

1

użyć właściwości contentSize z UITableView, że jak nie masz do podjęcia komórce height lub count pod uwagę.

- (void) updateTableInsetsForHeight: (CGFloat) height 
{ 
    CGFloat tableViewInset = MAX((height - self.tableView.contentSize.height)/2.0, 0.f); 
    self.tableView.contentInset = UIEdgeInsetsMake(tableViewInset, 0, -tableViewInset, 0); 
} 

Aktualizacja contentInsets po ponownym tableView -DANE i gdy zawierający widok tableView staje się widoczny (viewWillAppear:) oraz, gdy zmienia się rozmiar (viewWillTransitionToSize:withTransitionCoordinator:).

- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator 
{ 
    [self updateTableInsetsForHeight: size.height]; 
} 
1

Swift 3 na podstawie Pipiks odpowiedzieć

let headerHeight: CGFloat = (view.frame.size.height - CGFloat(Int(tableView.rowHeight) * tableView.numberOfRows(inSection: 0)))/2 
tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, -headerHeight, 0) 
-1

dla SWIFT 3:

var headerHeight: CGFloat = (tableView.frame.size.height - CGFloat(Int(tableView.rowHeight) * tableView.numberOfRows(inSection: 0)))/2 
if headerHeight > 0 { 
    tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, 0/*-headerHeight*/, 0) 
} else { 
    headerHeight = 0 
    tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, 0/*-headerHeight*/, 0) 
} 

WYNIK:

enter image description hereenter image description here enter image description hereenter image description here

Powiązane problemy