How to change the height of a BETWEEN section in a GROUPED UITableView?

The default height is too high for me. How to change the height between two sections?

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

Sorry for my poor English ... I mean, the gap between the two sections is too wide. How to change it?

- (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; } 

I changed the presentation of the header and footer of each section. And here is the result: enter image description here

I think there may be a minimum clearance height because I set the height of the header and footer to 2px, but the gap between the two sections is still so wide.

+8
ios iphone
source share
3 answers

You can use the footer views between each of your sections and provide them with the size of the required space by following these methods in your UITableView.

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

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

In the first delegate method, you can return a simple instance of UIView, and in the second, you can return the height of this view. This second delegate method should allow you to control the spacing between sections.

+14
source share

try this code

 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>; } 

and the Swift version is

 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) } 
+4
source share

You need to use the heightForHeaderInSection method to determine the space between the header and the text of the cell. You can also change it depending on different sections, for example. in some sections, you may need more distance, and under some, you do not want to show a space. In this case you can use

CGFLOAT_MIN, which is 0.000001f

. With an example of how you can use a different section with different header heights

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

Hope this helps you.

0
source share

All Articles