【发布时间】:2012-07-11 19:37:23
【问题描述】:
我一直在努力隐藏页脚视图。我的问题是,当我单击按钮时,页脚中有一个按钮章节更新后。
footerView.hidden = YES
我在按钮操作中使用了它,但它不起作用。
【问题讨论】:
标签: iphone uitableview ios5 footer
我一直在努力隐藏页脚视图。我的问题是,当我单击按钮时,页脚中有一个按钮章节更新后。
footerView.hidden = YES
我在按钮操作中使用了它,但它不起作用。
【问题讨论】:
标签: iphone uitableview ios5 footer
应该这样做
tableView.tableFooterView.hidden = YES;
【讨论】:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { [tableView.tableFooterView setHidden:YES]; return 1.0f; } 关键是返回 1.0f
有四种解决方案。他们是,
解决方案 1:
tableView.sectionHeaderHeight = 0.0;
tableView.sectionFooterHeight = 0.0;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger )section {
return 1.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger )section {
return 1.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )section {
return [[UIView alloc] initWithFrame:CGRectZero];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section {
return [[UIView alloc] initWithFrame:CGRectZero];
}
解决方案 2:
您可以通过界面生成器在尺寸选项卡下设置页脚/页眉高度。
解决方案 3:
设置 contentInset 属性。
self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, -20, 0);
用于使顶部和底部接触边缘。
解决方案 4:
实现以下,根据您的条件设置值。 0.0 将不被接受。较低的值应为 1.0。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger )section {
if(section == 0) {
return 6;
} else {
return 1.0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger )section {
return 5.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )section {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
【讨论】:
这个可以 通过实现委托方法
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
【讨论】:
不使用自定义视图时,从tableView:titleForFooterInSection: 返回nil,从tableView:heightForFooterInSection: 返回0。
@property(nonatomic, strong) NSString *footerTitle;
@property(nonatomic, assign) BOOL shouldHideFooter;
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return self.shouldHideFooter ? nil : self.footerTitle;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return self.shouldHideFooter ? 0 : UITableViewAutomaticDimension;
}
【讨论】:
Swift 3.0 解决方案,该解决方案还删除了前一个单元格和后一个单元格之间令人讨厌的 1px 边框。
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return nil
}
【讨论】:
参考@J.Hong的回答,Swift4版本(iOS 11.3):
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNonzeroMagnitude
}
与@Tim Newton 的回答相比:
titleForFooterInSection
CGFloat.leastNormalMagnitude -> .leastNonzeroMagnitude(更 Swifty IMO)【讨论】:
在 swift4 中:
self.tableView.tableFooterView = nil
【讨论】:
Swift 5 解决方案:(隐藏部分页脚,您可能需要另一块来摆脱表格页脚)
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.0
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
【讨论】: