简体   繁体   English

消除uitableview tablefooterview分隔符

[英]Eliminate uitableview tablefooterview separator

I have a uitableview with a tablefooterview. 我有一个uitableview和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. 我想在tableview中使用单元格分隔符,但想摆脱默认情况下在tableview和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 但都行不通

使用以下代码

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

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. 您可以在此处看到,如果设置了footerView,则具有plain样式的UITableView实际上确实会松开最后一个分隔符。

(Orange view is the footerView and there is no separator above him) (橙色视图是footerView,他上方没有分隔符)
Of course in Grouped tableView this does not work as discussed in comments under your question. 当然,在Grouped tableView中,它不能像您的问题下的注释中所讨论的那样工作。

在此处输入图片说明

https://gist.github.com/bartekchlebek/2b05b5ddcc3efec3f514 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

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. 编辑:我发现避免上一节和表页脚之间的间隙的一种更简单的方法是实现heightForFooterInSection并返回一个非常小的值(不为0)。这将导致不可见任何内容。

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. 出于某种原因,返回0不会抑制分组样式表视图中节的页脚的呈现,它将尝试呈现1点脚的页脚。 There is other discussion on StackOverflow about this. 关于StackOverflow还有其他讨论。

- (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). 我的技巧只是将所需的实际页脚视图放置在包装器视图的内部,框架位置为(0,-1)。

Let's say you have a table footer view that you want to use, a class called MyTableFooterView. 假设您有一个要使用的表脚视图,该类称为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. 有一种更惯用的方式,但我还没有遇到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM