-2

I have a UITableView with a variable number of rows, rather than being a static height.

How can I vertically center this variable-height table view within a UIView?

4

1 回答 1

0

在表格视图单元格中垂直居中文本的一种便宜且简单的方法是您可以将 NSTextFieldCell 子类化,并为每个表格列设置它以使用子类单元格。在子类中,截取绘制单元格内容的矩形是一件简单的事情:重写drawInteriorWithFrame方法,对框架进行一些调整,然后调用super方法。

- (void)drawInteriorWithFrame:(NSRect)cellFrame
                        inView:(NSView *)controlView {
    // Adjust the cell frame rect so it appears vertically centered.
    NSSize contentSize = [self cellSize];
    cellFrame.origin.y += (cellFrame.size.height - contentSize.height)
/ 2.0f;
    cellFrame.size.height = contentSize.height;
    // Do whatever it is it does...
    [super drawInteriorWithFrame:cellFrame inView:controlView];
}
于 2012-07-05T12:28:18.677 回答