iPhone UITableView:如何删除组样式表中各节之间的间距?

我正在创建一个表视图,其中有10个部分,都有标题视图但没有单元格。 因此,简而言之,我的表视图将仅显示10个标题视图; 任何部分都没有细胞。 现在,当我这样做时,部分的标题视图之间有一些空格。 我想删除那个空间。 那可能吗? 你能为我提供一些提示或解决这个问题吗?

以下是数据源方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 10; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 0; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UILabel * label = [[UILabel alloc] init]; label.backgroundColor = [UIColor grayColor]; return label; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 44.0f; } 

以下是输出的屏幕截图: 在此处输入图像描述

我有一个复杂的原因,为什么我这样做,我不能通过写作来解释。 我想要做的就是在部分的标题视图中没有间距。 先谢谢你。

尝试这个..

  self.tableView.rowHeight = 0; // in viewdidload [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; // in viewdidload -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01f; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return ; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ return [[UIView alloc] initWithFrame:CGRectZero]; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ return ; } 

还有table seprator as none。

如果在tableView:heightForFooterInSection:返回0 tableView:heightForFooterInSection:则返回默认值(可能是10.0)。 这很棘手,但您可以使用CGFLOAT_MIN而不是0来删除页脚。

 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } 

更新:

斯威夫特3

 func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return CGFloat.leastNormalMagnitude } 

尝试使用以下代码

 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.25; // as for you need } 

您也可以通过以下委托方法进行管理

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 

简而言之,您可以通过以上两种方法来管理它。

分组的样式表视图具有默认页脚,您还应该自定义页脚以覆盖它

要么

尝试朴素的风格。

 typedef enum { UITableViewStylePlain, UITableViewStyleGrouped } UITableViewStyle; 

您可以直接在界面中执行此操作,您必须将页脚高度设置为1,如下所示:

在此处输入图像描述

将表视图样式从Grouped更改为Plain。 这解决了我的问题。

所以我不能投票,但我可以发表回复

事实上这种方式的正确答案

 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } 

您需要使用方法heightForHeaderInSection。 您也可以根据不同的部分进行更改,例如。 在某些部分你可能需要显示更多的距离和一些,你不想显示差距。 对于这种情况,您可以使用0.000001f的CGFLOAT_MIN。 举个例子,如何使用不同的标题高度的不同部分

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 0 || section == 2) { return 55.0; } else { return CGFLOAT_MIN; } } 

希望它会对你有所帮助。

试试这个:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 0; } 

如评论中所述,请尝试使用此:

  self.tableView.rowHeight = 0;