Как установить UITableView всегда в центр UIView?
Я хотел бы реализовать UITableView
в центре UIView
. Первоначально он имеет только 2 или 3 ряда. Когда пользователь добавляет больше строк, он будет расширяться в вертикальном направлении, в то время как весь контент остается в центре, как показано ниже:
Можно ли это сделать с помощью UITableView
?
- Проблема производительности UISearchBar с основными данными
- Переупорядочение вращающейся uitableviewcell
- tableView: dequeueReusableCellWithIdentifier проблемы с «глобальной» константой
- Обработка пустого UITableView. Распечатать дружественное сообщение
- RXSwift Как создать оболочку для метода делегата с возвращаемым значением
- Вращающееся колесо «карт»,
- ios UITableViewCell программно создает в drawRect vs layoutSubviews
- indexPathForCell возвращает nil, поскольку ios7
- Шифрование UITableView с использованием NSFetchedResultsController
- Как изменить кнопку удаления по умолчанию в ячейке просмотра таблицы в Swift?
- Заполнение простого UITableView в MonoTouch с помощью UITableViewDataSource - как использовать UITableViewDataSource
- iOS - другой поток должен отправить reloadData в mainthread
- Проведите по удалению ячейки, чтобы tableViewHeader перемещался с помощью ячейки
Это можно сделать с использованием свойства contentOffset
UIScrollView .
-
Сделайте рамку таблицы TableView сидящей в границах:
tableView.frame = self.view.bounds; tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
-
Объявить метод -layoutTableView:
- (void)layoutTableView { CGSize contentSize = tableView.contentSize; CGSize boundsSize = tableView.bounds.size; CGFloat yOffset = 0; if(contentSize.height < boundsSize.height) { yOffset = floorf((boundsSize.height - contentSize.height)/2); } tableView.contentOffset = CGPointMake(0, yOffset); }
-
Когда вы вызываете
[tableView reloadData]
, просто вызывайте[self layoutTableView]
впоследствии.
Другим решением является настройка вставки содержимого табличного представления, так как решение смещения содержимого не работает для меня. Вот основная идея (вставлена в пользовательский подкласс UITableView):
- (void)reloadData { [super reloadData]; [self centerTableViewContentsIfNeeded]; } - (void)layoutSubviews { [super layoutSubviews]; [self centerTableViewContentsIfNeeded]; } - (void)centerTableViewContentsIfNeeded { CGFloat totalHeight = CGRectGetHeight(self.bounds); CGFloat contentHeight = self.contentSize.height; //If we have less content than our table frame then we can center BOOL contentCanBeCentered = contentHeight < totalHeight; if (contentCanBeCentered) { self.contentInset = UIEdgeInsetsMake(ceil(totalHeight/2.f - contentHeight/2.f), 0, 0, 0); } else { self.contentInset = UIEdgeInsetsZero; } }
Для Swift-hearted здесь игровая площадка:
import UIKit import Foundation import XCPlayground class CenteredTable: UITableView { override func reloadData() { super.reloadData() centerTableContentsIfNeeded() } override func layoutSubviews() { super.layoutSubviews() centerTableContentsIfNeeded() } func centerTableContentsIfNeeded() { let totalHeight = CGRectGetHeight(bounds) let contentHeight = contentSize.height let contentCanBeCentered = contentHeight < totalHeight if (contentCanBeCentered) { contentInset = UIEdgeInsets(top: ceil(totalHeight/2 - contentHeight/2), left: 0, bottom: 0, right: 0); } else { contentInset = UIEdgeInsetsZero; } } } class DataSource: NSObject, UITableViewDataSource { let items = ["Mr", "Anderson", "Welcome", "Back", "We", "Missed", "You"] func registerReusableViewsWithTable(tableView: UITableView) { tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell cell.textLabel?.text = items[indexPath.row] cell.textLabel?.textAlignment = .Center return cell } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count } } let dataSource = DataSource() let table = CenteredTable(frame: CGRectMake(0, 0, 300, 800), style: UITableViewStyle.Plain) table.tableFooterView = UIView(frame: CGRectZero) let container = UIView(frame: table.frame) container.addSubview(table) dataSource.registerReusableViewsWithTable(table) table.dataSource = dataSource table.reloadData() XCPShowView("table", container) container
Если вы не используете свои заголовки в таблице, вы можете динамически вычислять высоту ячеек по сравнению с высотой, связанной с таблицей.
Престижность https://stackoverflow.com/a/15026532/1847601
- (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; }
Swift 3
private func hightTableView() { yourTableView.frame = CGRect(x: 0, y: Int((Int(view.frame.height) - rowHeight * yourArrayData.count) / 2), width: widthTable, height: rowHeight * yourArrayData.count) }