【发布时间】:2011-03-17 21:18:17
【问题描述】:
我想在 UITableView 的页脚中显示一个 UIButton(应该与页眉完全相同)。
这段代码在我的 TableViewController 的 viewDidLoad 中:
UIButton *footerShareButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[footerShareButton setFrame:CGRectMake(10, 0, 70, 50)];
NSLog(@"%f", footerShareButton.frame.size.width);
[self.tableView setTableFooterView:footerShareButton];
NSLog(@"%f", footerShareButton.frame.size.width);
NSLog(@"%f", self.tableView.tableFooterView.frame.size.width);
但是,此代码不起作用。 Button 的宽度太大了,是 320。(虽然高度是正确的。)第一个 NSLog 输出 70,第二个和第三个输出 320。所以看起来 setTableFooterView 方法奇怪地调整了按钮的大小。
我已经解决了这个问题,方法是将 UIButton 放在另一个 UIView 中,然后再将其设置为表格页脚:
UIButton *footerShareButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[footerShareButton setFrame:CGRectMake(10, 0, 70, 50)];
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 70, 50)];
[aView addSubview:footerShareButton];
[self.tableView setTableFooterView:aView];
[aView release];
这行得通,按钮的宽度正确。
但是,我不明白为什么第一个代码示例不起作用。谁能解释一下?
【问题讨论】:
标签: iphone header uitableview footer