有没有办法减少 UITableView 两个部分之间的空间?我的每个部分之间大约有 15 个像素。我已经尝试为
-tableView:heightForFooterInSection:
和 -tableView:heightForHeaderInSection:
返回 0,但这不会改变任何东西。
有什么建议吗?
这有点棘手,但试试这个代码:
- (CGFloat)tableView:(UITableView*)tableView
heightForHeaderInSection:(NSInteger)section {
if (section == 0) {
return 6.0;
}
return 1.0;
}
- (CGFloat)tableView:(UITableView*)tableView
heightForFooterInSection:(NSInteger)section {
return 5.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];
}
相应地更改值。要删除空格,我认为 0.0 不会被接受。最小的合理值似乎是 1.0。
对于所有想要将距离缩小到 0 的人,你必须使用:
tableView.sectionHeaderHeight = 0.0;
tableView.sectionFooterHeight = 0.0;
因为 UITableViewDelegate 的服务仅从大于零的浮点数开始产生效果。
-(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] autorelease];
}
-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}
(使用 iOS 4.1 和 XCode 4.0.2)
您实际上可以在 Interface Builder 中的“尺寸”选项卡下设置页脚/页眉/单元格高度。默认情况下,页眉/页脚设置为 10.0。
您必须降低节页眉/页脚的高度。那么各部分之间的空间就会减少。
试试这个代码
它对我有用:-)
tableView.sectionHeaderHeight = 2.0;
tableView.sectionFooterHeight = 2.0;
您还可以降低故事板中节页脚和页眉的高度。在表格视图 -> 尺寸检查器中。转到截面高度。
默认情况下,普通样式表设置为 22,分组样式表设置为 10。您可以通过分别增加/减少页眉和页脚的值来配置值。
也许使用这个,0 将无法按预期工作
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return .leastNormalMagnitude
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNormalMagnitude
}
对我来说,问题是因为我使用的是分组表格视图样式(
UITableViewStyleGrouped
)。当我将其更改为普通样式 (UITableViewStylePlain
) 时,我的 tableView:heightForHeaderInSection:
方法是唯一确定每个节标题大小的方法。
iOS7 更新: 由于 ARC 自动发布更新,这里是编辑过的@Martin Stolz 代码。
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
if(section == 0)
return 6;
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)];
}
(使用iOS7,Xcode v5.0)
对我来说,这个问题只需使用以下代码即可解决,
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return section == 0 ? 1 : 20 // 20 is my other sections height
}
返回第一部分的1已经解决了问题。
要更改页眉/页脚空间,必须实施以下方法:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
并且
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
(使用相应方法改变页脚高度)
以下代码完全删除了节周围的空格:
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return nil
}
public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNonzeroMagnitude
}
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return .leastNonzeroMagnitude
}
选项 1 - 创建表视图时
tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0
选项 2 - 对于其他视图和部分更灵活// Top space for sections
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0
}
// Bottom space for sections
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}
tableView.estimatedSectionFooterHeight = 0
tableView.estimatedSectionHeaderHeight = 0