gpt4 book ai didi

ios - 如何在支持方向的情况下将 UITableView 部分页脚居中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:10:24 25 4
gpt4 key购买 nike

编辑:在弄乱了几天之后,我真正的问题如下: 1. UITableView是否占满整个view?
2. 如果是这样,它如何将单元格的边界设置为看起来只占据 View 的一部分。 3. 我如何获得单元格的边界——或者更准确地说,我如何知道单元格占据的可见区域的边界。 self.tableView.bounds.size.width 没有帮助,因为它返回 View 的宽度。谢谢。将之前的信息留在下面,以防它有助于使我的问题更清楚。

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

例子:

- (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(在详细 View /右侧),在横向时应为 351.5。但是当我使用 setCenter 或尝试根据该中心调整左边缘时,它不会居中。

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

最佳答案

要在 TableView 中居中放置某些内容,您需要将其包装在容器中,并为嵌入式 View 和容器设置适当的自动调整大小掩码。

容器应该是灵活的宽度,嵌入式 View 应该有两个灵活的侧边距。

例如:

- (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;
}

关于ios - 如何在支持方向的情况下将 UITableView 部分页脚居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6458373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com