當只有一個或兩個單元格時,如何在UITableViewCell中垂直居中單元格? Apple在他們的應用程序中經常這樣做,例如在設置應用程序中編輯日期。UITableView中垂直居中的UITableViewCell
下面是一個例子:
當只有一個或兩個單元格時,如何在UITableViewCell中垂直居中單元格? Apple在他們的應用程序中經常這樣做,例如在設置應用程序中編輯日期。UITableView中垂直居中的UITableViewCell
下面是一個例子:
我會考慮把一個大的,透明的UITableViewCell上面(ALA你如何與用戶的信息做在gitHubby,但沒有數據)。
這就是480像素高的好處。你可以放入一些東西,固定的大小,它只會工作。
或者將\ n \ n \ n包含的節標題放入,這也是不可見和透明的。
唉間諜++什麼的運行iOS應用:)
我去頭策略,動態計算內容高度居中表中的所有內容。這樣做的好處是,它會與任何表的內容或視圖大小工作:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
CGFloat contentHeight = 0.0;
for (int section = 0; section < [self numberOfSectionsInTableView: tableView]; section++) {
for (int row = 0; row < [self tableView: tableView numberOfRowsInSection: section]; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: row inSection: section];
contentHeight += [self tableView: tableView heightForRowAtIndexPath: indexPath];
}
}
return (tableView.bounds.size.height - contentHeight)/2;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame: CGRectZero];
view.backgroundColor = [UIColor clearColor];
return view;
}
偉大的解決方案.. –
夫特3:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
var contentHeight:CGFloat = 0.0
for section in (0 ..< numberOfSections(in: tableView)) {
for row in (0 ..< self.tableView(tableView, numberOfRowsInSection: section)) {
let indexPath = NSIndexPath(row: row, section: section)
contentHeight += self.tableView(tableView, heightForRowAt: indexPath as IndexPath)
}
}
return (tableView.bounds.size.height - contentHeight)/2;
}
我喜歡節頭中的方法。 :-D –
將\ n作爲該部分的標題的好主意,但這並不完美。一行太小,兩行太多。雖然有一個自定義大小的標題。 –
好吧,它不會只是與iPhone 5現在工作。任何更好的解決方案? – Dobler