19

我想在 aUITableView的中心实现 a UIView。最初它只有 2 或 3 行。当用户添加更多行时,它会沿垂直方向延伸,而整个内容保持在中心,如下图所示:

在此处输入图像描述

有可能做到这一点UITableView吗?

4

5 回答 5

8

可以使用UIScrollView 的contentOffset属性来完成。

  1. 使您的 tableView 的框架位于边界内:

    tableView.frame = self.view.bounds;
    tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    
  2. 声明一个方法-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);
    }
    
  3. 当您打电话时[tableView reloadData],请[self layoutTableView]稍后再打电话。

于 2012-12-25T15:17:00.073 回答
8

另一个解决方案是调整表格视图的内容插图,因为内容偏移解决方案对我不起作用。继承人的基本思想(插入自定义 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,这里有一个 Playground:

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
于 2015-06-22T15:08:04.137 回答
5

如果您不在 tableview 中使用标题,则可以动态计算单元格的高度与 tableviews 绑定高度相比。

感谢 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;
}
于 2015-05-21T08:59:17.390 回答
3

不需要那种笨重的逻辑。只需在中使用以下行cellForRowAt()

迅速:

tableView.contentInset = UIEdgeInsets(top: (tableView.bounds.height/2 - cell.bounds.height/2), left: 0, bottom: 0, right: 0)

目标-C:

tableView.contentInset = UIEdgeInsetsMake((tableView.bounds.height/2 - cell.bounds.height/2), 0, 0, 0);
于 2020-01-30T11:18:23.217 回答
2

斯威夫特 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)
}
于 2017-06-01T11:01:47.937 回答