Space UITableview 中的部分之间
Space Between Sections in UITableview
我需要减少 UITableView
两部分之间的 space。我查看了这个 question 但该解决方案不允许我的自定义页眉视图,因为它结合了页脚和页眉的 space。
这是 UITableView
的照片。黑色是 UITableView
背景色。
我认为您可以通过将页脚视图高度调整到最小值来解决此问题:在 Storyboard 或 XIB 中。
我不知道你在页脚高度的代码中写了什么。如果我错了请见谅。
可能与 Hide footer view in UITableView
重复
您是否尝试覆盖此功能:
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNormalMagnitude
}
您需要使用方法 heightForHeaderInSection
来定义 header 和单元格文本之间的 space。您也可以根据不同的部分进行更改,例如。在某些部分,您可能需要显示更多距离,而在某些部分,您不想显示间隙。对于这种情况,您可以使用 CGFLOAT_MIN
,即 0.000001f。举个例子,如何使用具有不同 header 高度的不同部分:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0 || section == 2)
{
return 55.0;
}
else
{
return CGFLOAT_MIN;
}
}
您可以通过实现委托 heightForHeaderInSection
& heightForFooterInSection
.
return值不能为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;
}
对于Swift 3
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
这也可能有帮助:
override func viewDidLoad() {
super.viewDidLoad()
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
}
除了 之外,我想补充一点,您还需要实施 tableView:viewForFooterInSection:
方法,为要删除空 [=16= 的部分返回 nil
] 以下
它将变成:
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return nil;
}
TableView 委托方法对浮点值为 0.0f 无效。尝试提供一个大于该值的值。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.00001f;
}
- (UIView*)tableView:(UITableView*)tableView
viewForFooterInSection:(NSInteger)section {
return [[UIView alloc] initWithFrame:CGRectZero];
}
对于Swift4+你需要实现这两个方法
extension MyViewController : UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
}
对于Swift 5+:
默认情况下,页眉和页脚有一些 space。这就是为什么我在为部分设置精确分隔时遇到问题。
我将 2 个部分分开的解决方案如下:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 24
} else if section == 1 {
return 32
} else {
return 40
}
}
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,我需要 return nil。
如果您只想更改 footerHeight,只需 return CGFloat.leastNormalMagnitude heightForHeaderInSection,return heightForFooterInSection 中每个部分的高度。
Select storyboard/objectCode 中的 tableView
并确保样式设置为 Plain
,而不是 Grouped
。您可以在属性“检查器”选项卡中找到此设置。
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
}()
与其实施 UITableViewDelegate
方法并通过 CGFloat.leastNormalMagnitude
定义 sectionFooterHeight,还可以选择
tableView.sectionFooterHeight = 0
没有页脚时各部分之间的间距将消失。
机制是默认这个值设置为UITableView.automaticDimension
.
只要
- 它保持
UITableView.automaticDimension
- 没有 delegate/dataSource 实现页脚配置的方法,即
titleForFooterInSection
/viewForFooterInSection
- table 视图的样式设置为
.grouped
然后 UITableView 会故意在没有视图的部分之间插入一个间距。
您将 sectionFooterHeight
更改为 0,魔法消失。
在 iOS 15 你可能想要减少 sectionHeaderTopPadding
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
}
在 Xcode 13.2 中,您可以调整情节提要中各个部分的页眉和页脚的高度 - 请参见下面的屏幕截图:
为我工作
tableView.sectionFooterHeight = 10
// ...
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
我只需要减少 tableview 部分的顶部填充 header:
tableView.sectionHeaderTopPadding = 0
swift 5 iOS 15
self.tableView.estimatedSectionFooterHeight = 16.0 // spacing between Sections
我需要减少 UITableView
两部分之间的 space。我查看了这个 question 但该解决方案不允许我的自定义页眉视图,因为它结合了页脚和页眉的 space。
这是 UITableView
的照片。黑色是 UITableView
背景色。
我认为您可以通过将页脚视图高度调整到最小值来解决此问题:在 Storyboard 或 XIB 中。
我不知道你在页脚高度的代码中写了什么。如果我错了请见谅。
可能与 Hide footer view in UITableView
重复您是否尝试覆盖此功能:
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNormalMagnitude
}
您需要使用方法 heightForHeaderInSection
来定义 header 和单元格文本之间的 space。您也可以根据不同的部分进行更改,例如。在某些部分,您可能需要显示更多距离,而在某些部分,您不想显示间隙。对于这种情况,您可以使用 CGFLOAT_MIN
,即 0.000001f。举个例子,如何使用具有不同 header 高度的不同部分:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0 || section == 2)
{
return 55.0;
}
else
{
return CGFLOAT_MIN;
}
}
您可以通过实现委托 heightForHeaderInSection
& heightForFooterInSection
.
return值不能为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;
}
对于Swift 3
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
这也可能有帮助:
override func viewDidLoad() {
super.viewDidLoad()
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
}
除了 tableView:viewForFooterInSection:
方法,为要删除空 [=16= 的部分返回 nil
] 以下
它将变成:
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return nil;
}
TableView 委托方法对浮点值为 0.0f 无效。尝试提供一个大于该值的值。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.00001f;
}
- (UIView*)tableView:(UITableView*)tableView
viewForFooterInSection:(NSInteger)section {
return [[UIView alloc] initWithFrame:CGRectZero];
}
对于Swift4+你需要实现这两个方法
extension MyViewController : UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
}
对于Swift 5+:
默认情况下,页眉和页脚有一些 space。这就是为什么我在为部分设置精确分隔时遇到问题。
我将 2 个部分分开的解决方案如下:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 24
} else if section == 1 {
return 32
} else {
return 40
}
}
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,我需要 return nil。
如果您只想更改 footerHeight,只需 return CGFloat.leastNormalMagnitude heightForHeaderInSection,return heightForFooterInSection 中每个部分的高度。
Select storyboard/objectCode 中的 tableView
并确保样式设置为 Plain
,而不是 Grouped
。您可以在属性“检查器”选项卡中找到此设置。
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
}()
与其实施 UITableViewDelegate
方法并通过 CGFloat.leastNormalMagnitude
定义 sectionFooterHeight,还可以选择
tableView.sectionFooterHeight = 0
没有页脚时各部分之间的间距将消失。
机制是默认这个值设置为UITableView.automaticDimension
.
只要
- 它保持
UITableView.automaticDimension
- 没有 delegate/dataSource 实现页脚配置的方法,即
titleForFooterInSection
/viewForFooterInSection
- table 视图的样式设置为
.grouped
然后 UITableView 会故意在没有视图的部分之间插入一个间距。
您将 sectionFooterHeight
更改为 0,魔法消失。
在 iOS 15 你可能想要减少 sectionHeaderTopPadding
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
}
在 Xcode 13.2 中,您可以调整情节提要中各个部分的页眉和页脚的高度 - 请参见下面的屏幕截图:
为我工作
tableView.sectionFooterHeight = 10
// ...
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
我只需要减少 tableview 部分的顶部填充 header:
tableView.sectionHeaderTopPadding = 0
swift 5 iOS 15
self.tableView.estimatedSectionFooterHeight = 16.0 // spacing between Sections