I have a grouped table view with a few sections. By default, there is gap between the sections. I've been looking around for a simple way to remove the gap completely without any luck. The best I could do through the Interface Builder is to set the "Section Height" values in the "Table View Size" property section, but these fields do not accept 0 as valid input. Can anyone tell me the best way to sort this out?
The above answer for me did not work, I had to actually create a UIView with a height of 1 and a background color of clear and then set it's y origin coordinate to -1.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -1, tableView.frame.size.width, 1)];
[view setBackgroundColor:[UIColor clearColor]];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 1;
}
This gave me a Grouped tableView where the section cells would not scroll underneath the headerView and not have padding in-between the sections.
In your table view's delegate, you can implement tableView:heightForHeaderInSection: and tableView:heightForFooterInSection:.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.0;
}
Related
I have created a UITableView using the Interface Builder I have a section footer space there, I have designed my custom view as the section footer. But I want the section footer to be hidden initially and only load it after a service call.
I have tried
self.tableview.tablefooterview.hidden = YES
self.tableview.sectionFooterHeight = 0.0f
I have also set the height in delegate methods, but the table footer view is not hiding at all. How do I hide the table footer view.
check this Link for how to Hide footer view in UITableView
- (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] initWithFrame:CGRectZero];
}
Use UITableView delegate method :-
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0;
}
Please try to set section footer value to more than 0(zero). I tried to set it as 0.000001 and it works.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.000001;
}
Please try this code
- (void)viewDidLoad
{
self.tableview.sectionHeaderHeight = 0.0;
self.tableview.sectionFooterHeight = 0.0;
self.tableview.tableHeaderView=[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableview.bounds.size.width, 0.01f)];
self.tableview.tableFooterView=[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableview.bounds.size.width, 0.01f)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0;
}
I have a UITableView set to the UITableViewStyleGrouped style. Whenever a section has one or more items, it displays a one pixel separator beneath the section header. However, if the section has no items, there is no separator between sections.
How can I get the separator to appear in all sections, even those that have no cells?
Notice how section 1 and 2 have a separator between them and their first cell, but section 3 doesn't have a separator between it and section 4.
One solution is to add a footer to each empty section:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return [self tableView:tableView numberOfRowsInSection:section] == 0 ? 0.5 : 0.00000001;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if ([self tableView:tableView numberOfRowsInSection:section] == 0) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.5)];
view.backgroundColor = tableView.separatorColor;
return view;
} else {
return nil;
}
}
Notes:
This uses the separatorColor to ensure it is consistent with the other lines in the table view.
Instead of returning 0 in heightForFooterInSection, it returns a very small number instead.
This produces correct results most of the time:
... but has several problems:
This exploits the footer, so if you need to display a footer, this solution probably won't work.
In edit mode, when you drag the last item out of a section, the border will disappear between that section and the one below it.
In edit mode, when you drag an item into an empty section, there will be a double border at the bottom of the section.
Example of problems 2 and 3:
In the above image, notice how there is no line between sections 2 and 3, since all the items were moved out of that section (problem 2).
Also notice how the last items in section 3 and 4 have double borders, since they were dragged into new sections (problem 3).
You can try customize your section header view. use this delegate funciton: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
Use this two method to add separator between Section.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44.0;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor brownColor]];
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 43, 320, 1)];
img.backgroundColor = [UIColor whiteColor];
[headerView addSubview:img];
return headerView;
}
I have successfully removed the colored separator at the bottom of my UITableView with:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.1f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [UIView new];
//[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
However when I deselect the last row, the footer gets drawn again.
How can I prevent the footer in my section 1 from ever being drawn please?
This code creates a margin at the top and bottom of the UITableView. How can I modify it to do the same but between each individual UITableViewCell?
// set header height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10;
}
// set header colour
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor lightGrayColor];
return headerView;
}
// set footer height
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 10;
}
// set footer colour
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor lightGrayColor];
return headerView;
}
If successful, I'd also like to be able to add similar left and right margins if possible.
Use an extra Background UIView with top, left. bottom and right margin.[red background view in the picture]. Add all other subViews on this view.
For my UITableView I have such code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
But when I launch me app, it shows an extra separator. It's not additional cell - it cannot be selected. How can it be fixed?
Eliminate extra separators below UITableView
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [UIView new];
// If you are not using ARC:
// return [[UIView new] autorelease];
}
With iOS7
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
It is by default in UITableView. You can use templates to create the kind of view you need, in an empty view controller (or) change the frame size of your UITableView in code by creating and allocating your own view.
You can also try to set footer view height to zero, by using next code in UITableViewDelegate class:
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0;
}