2

I have a uitableview with a tablefooterview. I would like use cell separators within the tableview but would like to get rid of the full screen width separator placed by default between the tableview and the tablefooterview. I have tried

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
    [cell setLayoutMargins:UIEdgeInsetsZero];
}

and

cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);

but neither works

scribbler2309
  • 153
  • 1
  • 11

3 Answers3

0

Use following code

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Arun Gupta
  • 2,628
  • 1
  • 20
  • 37
  • I have an actual tablefooterview that I would like to use so this will not help – scribbler2309 Nov 20 '14 at 17:30
  • You can pass a [[UIView alloc] initWithFrame:CGRectZero]; to footerView – Arun Gupta Nov 20 '14 at 17:51
  • @scribbler2309 If you already have the footer view you want to use, why didn't you just set it? – yusuke024 Nov 20 '14 at 18:11
  • I set it. The table view creates a full screen separator line for reasons unclear to me – scribbler2309 Nov 20 '14 at 20:42
  • @ArunGupta I have an actual view for the footer with labels and buttons and other things that I am setting as my UITableView footerView, there is a single separator line that persists – scribbler2309 Nov 20 '14 at 21:45
  • Can you post your FooterView method and how the tableview screen looks now and what you want it to be. This will give clear idea on what needs to be done. – Arun Gupta Nov 21 '14 at 05:04
  • hw can i detect o'clock below footer while i implement self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero – Nischal Hada Jul 09 '15 at 17:48
0

Have a look at this sample implementation.
You can see here that UITableView with plain style does in fact loose the last separator if a footerView is set.

(Orange view is the footerView and there is no separator above him)
Of course in Grouped tableView this does not work as discussed in comments under your question.

enter image description here

https://gist.github.com/bartekchlebek/2b05b5ddcc3efec3f514

#import "ViewController.h"

@interface ViewController () <UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                                        style:UITableViewStylePlain];
  tableView.dataSource = self;
  tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

  UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
  footerView.backgroundColor = [UIColor orangeColor];
  tableView.tableFooterView = footerView;

  [self.view addSubview:tableView];
}

#pragma mark - UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
  cell.textLabel.text = [NSString stringWithFormat:@"Row: %@", @(indexPath.row)];
  return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return 20;
}

@end
Bartek Chlebek
  • 1,665
  • 1
  • 16
  • 23
0

Edit: I have found that a much simpler way to avoid the gap between the last section and the table footer is to implement heightForFooterInSection and return a very small value that is not 0. This will result in nothing being visible.

For some reason, returning 0 doesn't suppress the rendering of section footers in a grouped-style table view, and it will try to render a 1-point footer. There is other discussion on StackOverflow about this.

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

I was able to accomplish this in what feels like a slightly hacky way, but it seems to work fine.

My hack is simply to place the actual footer view you want inside of a wrapper view, with a frame position of (0, -1).

Let's say you have a table footer view that you want to use, a class called MyTableFooterView.

In that case you can do something like this:

//This is the view we will actually add as the table footer
//We make it one point shorter since the content will be shifted up 
//one point out of its frame
CGRect footerFrame = CGRectMake(0, 0, self.tableView.width, myFooterHeight-1);
UIView* tableFooterWrapper = [[UIView alloc] initWithFrame:footerFrame];

//This is the view that we are trying to use as a table footer. 
//We place it 1 point up inside the wrapper so it overlaps that pesky 1-point separator.
CGRect contentFrame = CGRectMake(0, -1, self.tableView.width, myFooterHeight);
UIView* footerContent = [[MyTableFooterView alloc] initWithFrame:contentFrame];


//Make sure the footer expands width for to other screen sizes
footerContent.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[tableFooterWrapper addSubview:footerContent];
self.tableView.tableFooterView = tableFooterWrapper;

This works reliably for me. It's possible that there's a more idiomatic way, but I haven't come across one yet.

Jason LeBrun
  • 13,037
  • 3
  • 46
  • 42