2

我有一个带有 tablefooterview 的 uitableview。我想在 tableview 中使用单元格分隔符,但想摆脱默认放置在 tableview 和 tablefooterview 之间的全屏宽度分隔符。我努力了

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

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

但两者都不起作用

4

3 回答 3

0

看看这个示例实现。
您可以在这里看到,plain如果设置了 footerView,带有样式的 UITableView 实际上会丢失最后一个分隔符。

(橙色视图是footerView,上面没有分隔符)
当然,在GroupedtableView中,这不像您问题下的评论中所讨论的那样起作用。

在此处输入图像描述

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
于 2014-11-20T22:06:23.367 回答
0

编辑:我发现一个更简单的方法来避免最后一部分和表页脚之间的差距是实现 heightForFooterInSection 并返回一个非常小的值而不是 0。这将导致什么都不可见。

出于某种原因,返回 0 不会抑制在分组样式表视图中呈现节页脚,它会尝试呈现 1 点页脚。StackOverflow 上有关于此的其他讨论。

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

我能够以一种有点 hacky 的方式完成此任务,但它似乎工作正常。

我的技巧只是将您想要的实际页脚视图放置在包装视图中,框架位置为 (0, -1)。

假设您有一个要使用的表格页脚视图,一个名为 MyTableFooterView 的类。

在这种情况下,您可以执行以下操作:

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

这对我来说很可靠。可能有一种更惯用的方式,但我还没有遇到过。

于 2015-03-12T01:40:35.773 回答
0

使用以下代码

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
于 2014-11-20T17:24:29.920 回答