2014-11-20 107 views
2

我有一个tablefooterview的uitableview。我想在tableview中使用单元格分隔符,但是希望摆脱tableview和tablefooterview之间默认放置的全屏宽度分隔符。我曾尝试消除uitableview tablefooterview分隔符

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

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

,但下面的代码既不作品

+0

正在为'UITableViewStylePlain'使用'UITableView'吗?这将解决您的问题。 – 2014-11-20 16:42:17

+0

@BartekChlebek TableView是UITableViewStylePlain,仍然有tableview分隔符 – scribbler2309 2014-11-20 21:44:52

回答

0

使用

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

我有一个实际的tablefooterview,我想使用,所以这不会帮助 – scribbler2309 2014-11-20 17:30:38

+0

你可以传递[[UIView alloc] initWithFrame:CGRectZero];到页脚查看 – 2014-11-20 17:51:29

+0

@ scribbler2309如果你已经有了你想要使用的页脚视图,为什么你没有设置它? – sikhapol 2014-11-20 18:11:01

0

看一看这个样本实现。
您可以在这里看到,使用plain样式的UITableView实际上在设置了footerView后实际上会丢失最后一个分隔符。

(橙色的观点是footerView并且上面有他无分离器)
当然在Grouped的tableView在您的问题下方的评论中讨论这不起作用。

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 
0

编辑: 我发现一个更简单,以避免最后一节和表尾之间的差距的方法就是推行heightForFooterInSection并返回一个很小的值那不是0.这将导致没有任何东西可见。

由于某些原因,返回0不会抑制分组样式表视图中的分节页脚渲染,并且它将尝试渲染单点页脚。有关于此的StackOverflow的其他讨论。

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

我能在什么感觉就像一个稍微哈克的方式做到这一点,但它似乎很好地工作。

我的黑客只是简单地将你想要的实际页脚视图放在包装视图中,帧位置为(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; 

这可靠地工作对我来说。有可能是更习惯的方式,但我还没有遇到过。