如何使UITableView部分页脚居中并支持方向?

时间:2011-06-23 17:45:34

标签: ios uitableview orientation tableview footer

编辑: 在搞砸了几天后,我遇到的真正问题如下:  1. UITableView是否占用整个视图?
 2.如果是这样,它如何设置单元格的边界,看起来它只占用视图的一部分。  3.如何获得细胞的边界 - 或者更准确地说,我如何知道细胞占据的可见区域的边界。 self.tableView.bounds.size.width无效,因为它返回视图的宽度。 谢谢。 留下以前的信息,以防它有助于使我的问题更清楚。

这可能吗? 我已经阅读了苹果文档,并在这里和其他地方拖累了论坛,无法找到并回答这个问题。 无论你做什么,UITableVIew中的页脚是否实际占据了整个视图?它没有表宽度的概念吗?

示例:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    [footerView setBackgroundColor:[UIColor redColor]];

    return footerView;
}

此代码将从一条边到另一条边创建一条红线。无论你给它什么边界,线都将占据整个视图。这样做的问题是,如果您想要在该页脚中居中标签,那么如果您支持方向更改,则无法知道中心位置。 例如,在iPad应用程序中,我尝试执行以下操作:

if ([footerText length] > 0) {
    UIView *customView = [[[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 0, 0.0)] autorelease];

    [customView setBackgroundColor:[UIColor grayColor]];

    UILabel *footerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    footerLabel.numberOfLines = 2;
    footerLabel.adjustsFontSizeToFitWidth = NO;
    [footerLabel setTextAlignment:UITextAlignmentCenter];
    [footerLabel setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]];
    [footerLabel setOpaque:NO];
    [footerLabel setTextColor:[UIColor whiteColor]];
    [footerLabel setShadowColor:[UIColor lightGrayColor]];
    [footerLabel setFont:[UIFont boldSystemFontOfSize:14]];
    [footerLabel setFrame:CGRectMake(customView.center.x/0.3, 0.0, 600, 40.0)];
    [footerLabel setText:footerText];
    [customView addSubview:footerLabel];
    [footerLabel release];

    NSLog(@"customView width = %f", customView.frame.size.width);
    NSLog(@"tableview width = %f", self.tableView.frame.size.width);
    NSLog(@"tableview center = %f", self.tableView.center.x);

    return customView;
} else {
    return nil;
}

桌子的纵向中心应为384(位于详细视图/右侧),横向为351.5。但是当我使用setCenter或尝试根据该中心调整左边缘时,它不会居中。

最后一个问题:当页脚似乎没有表格边界的概念时,如何在页脚中居自定义视图并支持方向?我必须在文档中遗漏一些东西,因为这必须是其他人解决的问题,但我找不到它。 谢谢你的时间。

1 个答案:

答案 0 :(得分:7)

要将某些内容置于tableview中,您需要将其包装在容器中,并为嵌入视图和容器设置适当的自动调整大小。

容器的宽度应该是灵活的,嵌入式视图应该具有灵活的侧边距。

例如:

- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    static UIView *footerView;

    if (footerView != nil)
        return footerView;

    NSString *footerText = NSLocalizedString(@"Some Display Text Key", nil);

    // set the container width to a known value so that we can center a label in it
    // it will get resized by the tableview since we set autoresizeflags
    float footerWidth = 150.0f;
    float padding = 10.0f; // an arbitrary amount to center the label in the container

    footerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, footerWidth, 44.0f)];
    footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    // create the label centered in the container, then set the appropriate autoresize mask
    UILabel *footerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)] autorelease];
    footerLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    footerLabel.textAlignment = UITextAlignmentCenter;
    footerLabel.text = footerText;

    [footerView addSubview:footerLabel];

    return footerView;
}