我需要减少UITableView
两个部分之间的空间。我查看了这个question,但解决方案不允许我的自定义标题视图,因为它结合了页脚和标题的空间。
以下是UITableView
的图片。黑色是UITableView
背景色。
答案 0 :(得分:95)
您是否尝试覆盖此功能:
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.00001
}
答案 1 :(得分:36)
我认为您可以通过将页脚视图高度调整为其最小值来解决此问题:在Storyboard或XIB中。
我不知道你在代码中写的页脚高度。对不起,如果我错了。
可能重复答案 2 :(得分:12)
对于Swift 3
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
答案 3 :(得分:5)
你需要使用方法heightForHeaderInSection来定义header&之间的空间。细胞文本。您也可以根据不同的部分进行更改,例如。在某些部分,您可能需要显示更多距离&在某些情况下,你不想表现出差距。对于这种情况,您可以使用0.000001f的CGFLOAT_MIN。举个例子,你如何使用不同的标题高度的不同部分
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0 || section == 2)
{
return 55.0;
}
else
{
return CGFLOAT_MIN;
}
}
希望它会对你有所帮助。
答案 4 :(得分:4)
与answer posted by Icaro一起,我想补充一点,您还需要实现tableView:viewForFooterInSection:
方法,该方法返回nil
来删除下面的空白区域
然后它将变为:
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return nil;
}
答案 5 :(得分:2)
对于Swift 5 +:
默认情况下,页眉和页脚有一些空间。这就是为什么我要为各节设置一个精确的间距的原因。
我要在两个部分之间进行分隔的解决方案如下:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 24
} else if section == 1 {
return 32
}
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
nil
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
nil
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
CGFloat.leastNormalMagnitude
}
正如您所看到的viewForFooterInSection和viewForHeaderInSection一样,我需要返回nil。
如果只想更改footerHeight,只需为heightForHeaderInSection返回CGFloat.leastNormalMagnitude,然后在heightForFooterInSection中返回每个节的高度。
答案 6 :(得分:1)
这也可能有所帮助:
override func viewDidLoad() {
super.viewDidLoad()
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
}
答案 7 :(得分:1)
TableView委托方法在float值为0.0f时不起作用。尝试提供大于该值的值。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.00001f;
}
- (UIView*)tableView:(UITableView*)tableView
viewForFooterInSection:(NSInteger)section {
return [[UIView alloc] initWithFrame:CGRectZero];
}
答案 8 :(得分:0)
您可以通过实施委托heightForHeaderInSection
& heightForFooterInSection
。
返回值不应为0,即使SectionHeader或SectionFooter的高度为0,也需要非常小的值,请尝试CGFLOAT_MIN
。
我的例子:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == [self.dataArray indexOfObject:self.bannerList]) {
return 46;
}
return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
答案 9 :(得分:0)
对于Swift 4+,您需要覆盖这两种方法
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
答案 10 :(得分:0)
在情节提要/ objectCode中选择tableView,并确保将样式设置为“普通”,而不是“分组”。您可以在属性“检查器”选项卡中找到此设置。
let myTableView : UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.register(TableCellClass.self, forCellReuseIdentifier: "cellId")
tableView.backgroundColor = UIColor(red: 123/255, green: 190/255, blue: 120/255, alpha: 1)
tableView.separatorStyle = .none
tableView.translatesAutoresizingMaskIntoConstraints = false
return tableView
}()
答案 11 :(得分:0)
与其实现 UITableViewDelegate
方法并通过 CGFloat.leastNormalMagnitude
定义 sectionFooterHeight,不如直接
tableView.sectionFooterHeight = 0
并且当没有页脚时节之间的间距将消失。
机制是默认情况下将此值设置为 UITableView.automaticDimension
。
只要
UITableView.automaticDimension
titleForFooterInSection
/viewForFooterInSection
.grouped
然后 UITableView 会故意在没有视图的部分之间插入一个间距。
您将 sectionFooterHeight
更改为 0,魔法就会消失。