【问题标题】:How to center UITableView Section Footer with Support for Orientation?如何在支持方向的情况下使 UITableView 部分页脚居中?
【发布时间】:2011-09-21 10:48:30
【问题描述】:

编辑: 在搞砸了几天之后,我遇到的真正问题如下: 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 或尝试根据该中心调整左边缘时,它不会居中。

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

【问题讨论】:

    标签: ios uitableview orientation tableview footer


    【解决方案1】:

    要在 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;
    }
    

    【讨论】:

    • 诅咒! UIViewAutoresizingFlexibleWidth 是问题所在!当它是我应该看到的东西时讨厌它。谢谢codelark!
    猜你喜欢
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 2014-06-22
    • 1970-01-01
    • 2016-04-19
    相关资源
    最近更新 更多