如何更改GROUPED UITableView中的BETWEEN部分的高度?

问题描述:

默认的高度对我来说太大了。 如何更改两节之间的高度?如何更改GROUPED UITableView中的BETWEEN部分的高度?

============================================== ==============================

对不起,我可怜的英语...... 我的意思是两段之间的差距太大了。如何改变它?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    CGRect frame = CGRectMake(0, 0, tableView.frame.size.width, 2.0f); 
    UIView *view = [[[UIView alloc] initWithFrame:frame] autorelease]; 
    view.backgroundColor = [UIColor blueColor]; 
    return view; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 
{ 
    CGRect frame = CGRectMake(0, 0, tableView.frame.size.width, 2.0f); 
    UIView *view = [[[UIView alloc] initWithFrame:frame] autorelease]; 
    view.backgroundColor = [UIColor redColor]; 
    return view; 
} 

我改变了每个部分的页眉视图和页脚视图。 这里是结果: enter image description here

我觉得有可能是空缺的最低高度,因为我设置页眉和页脚为2px的高度,但两个部分之间的差距仍然很大。

+0

你的问题根本不清楚,你的意思是让两个部分之间的间隙变小,或者改变一个部分的高度而另一个部分保持相同的大小? – 2012-03-22 13:53:01

+0

对不起我的英文不好。我的意思是两部分之间的差距。 – 2012-03-22 14:58:37

您可以在每个部分之间使用页脚视图,并通过在UITableView的委托中实现以下方法为它们提供所需空间的大小。

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

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

在第一个委托方法可以返回一个简单的UIView实例,并在第二个你可以返回的高度,这个观点应该是。这第二个委托方法应该允许您控制节之间的差距。

+1

是的,我认为有一个最小距离。谢谢:) – 2012-03-22 15:08:16

+1

tableFooterView属性不引用节之间的页脚视图。 – 2013-03-28 11:28:27

+0

的确如此。 tableFooterView是放置在UITableView最底部的视图。代表使用' - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section'方法可以提供每个节的页脚视图。我更新了这个更正的答案。谢谢。 – 2013-03-28 12:16:36

尝试这个代码

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 <your header height>; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ 
    return [[UIView alloc] initWithFrame:CGRectZero]; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 
    return <your header view>; 
} 

和雨燕的版本是

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 
    return your footer view 
} 

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 

    return your footer view height 
} 

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 0.01 
} 

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    return UIView.init(frame: CGRect.zero) 
} 

您需要使用方法heightForHeaderInSection的头&单元格文本之间界定的空间。您也可以根据不同的部分对其进行更改,例如。在某些部分,您可能需要显示更多的距离&下的一些,你不想显示的差距。对于这样的情况下,你可以使用

CGFLOAT_MIN这是0.000001f

。举个例子,你可以如何使用具有不同头部高度的不同部分

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

希望它能帮助你。