如何将UITableView永远放在UIView的中心?

jus*_*nny 18 iphone uitableview ios

我想UITableView在一个中心实现一个UIView.最初它只有2或3行.当用户添加更多行时,它将在垂直方向上延伸,而整个内容保留在中心,如下所示:

在此输入图像描述

有可能这样做UITableView吗?

Dan*_*sko 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于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
Run Code Online (Sandbox Code Playgroud)


aka*_*kyy 7

可以使用UIScrollView的contentOffset属性完成.

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

    tableView.frame = self.view.bounds;
    tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    
    Run Code Online (Sandbox Code Playgroud)
  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);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 打电话时[tableView reloadData],请[self layoutTableView]稍后打电话.

  • 对于这种情况,我认为`tableView.contentInset`比`tableView.contentOffset`效果更好.它使内容在滚动后返回到中心,其中`contentOffset`只是更改滚动的位置.`tableView.contentInset = UIEdgeInsetsMake(yOffset,0.f,yOffset,0.f);` (3认同)

小智 5

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

感谢 /sf/answers/1051857271/

- (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;
}
Run Code Online (Sandbox Code Playgroud)