UITableView:显示最后一个单元格的分隔符的正确方法

问题是什么是在表格/部分的最后一个单元格中显示分隔符的最右边的方法。

基本上这是我所追求的。

在这里输入图像说明 这是从原生的音乐应用程序,这使我认为应该可以通过UITableView实现,他们不会使用一些私人API的细胞分离器,对不对?

我知道你可以不用实际的分隔符,但在单元格的底部添加一条像素线。 但我不是这种方法的粉丝,因为

  1. 当一个单元格被选中/突出显示时, 分隔符 前一个单元格的分隔符会自动隐藏(请参阅第二个屏幕截图,选中“您必须疯了”)。 这是我想要UITableView来处理,而不是自己做,如果我使用一个像素线(这是特别方便的单元格分隔符不会一直延伸到表视图的边缘,分隔符和选定的单元格背景不看一起很好)。
  2. 我想尽可能保持单元格的滚动性能。

也有一些UITableView引用,让我觉得有一个简单的方法来得到我想要的:

在iOS 7和更高版本中,单元格分隔符不会一直延伸到表视图的边缘。 该属性为表格中的所有单元格设置默认的插入,就像rowHeight设置单元的默认高度。 它还用于pipe理在普通样式表底部绘制的“额外”分隔符。

有人知道如何使用在普通样式表底部绘制的这些“额外”分隔符吗? 因为这正是我所需要的。 我认为分配给UITableView separatorInset ,而不是UITableViewCell会做的伎俩,但它不,最后一个单元格仍然缺less其分隔符。

现在我只能看到一个选项:有一个自定义的页脚模仿最后一个单元格的分隔符。 这是不好的,特别是如果你想要使用tableView:titleForFooterInSection:方法有一个实际的部分页脚。

这工作完美地为我添加一个分隔符到最后一个单元格。 玩的开心!

 self.tableView.tableFooterView = [[UIView alloc] init]; 

我刚刚find了适合我的东西。 简而言之: 给你的UITableView一个tableFooterView并将其框架的高度设置为0 。 这使得实际的分隔符显示,正确的插入。

更多细节:如果您使用的是故事板,将UIView拖动到Table View的底部(在左侧的树视图中),使其显示在Table View Cell的正下方,在同一个层次上。 这创build了一个tableFooterView。 当然这也可以通过编程来完成。

然后,在你的UITableViewController的实现中:

 UIView *footerView = self.tableView.tableFooterView; CGRect footerFrame = footerView.frame; footerFrame.size.height = 0; [footerView setFrame:footerFrame]; 

让我知道如果这对你有用! 它可能也适用于第一个分隔符,如果你使用tableHeaderView,我没有尝试过。

这将做你想要的东西..即使这条线将采取单元格的整个宽度:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; self.tableView.separatorColor = [UIColor redColor]; self.tableView.separatorInset = UIEdgeInsetsZero; 

我有一个类似的问题,并find一个解决方法。 苹果隐藏最后的uitableviewcell分隔符,然后显示它,如果你select单元格,或者如果你调用select和取消select该单元格。

所以我教最好的是- (void)layoutSubviews 。 分隔视图叫做_separatorView ,它是一个私有属性。 使用单元格的选定/突出显示的状态,你可以想出这样的事情:

 - (void)layoutSubviews { // Call super so the OS can do it's layout first [super layoutSubviews]; // Get the separator view UIView *separatorView = [self valueForKey:@"_separatorView"]; // Make the custom inset CGRect newFrame = CGRectMake(0.0, 0.0, self.bounds.size.width, separatorView.frame.size.height); newFrame = CGRectInset(newFrame, 15.0, 0.0); [separatorView setFrame:newFrame]; // Show or hide the bar based on cell state if (!self.selected) { separatorView.hidden = NO; } if (self.isHighlighted) { separatorView.hidden = YES; } } 

像这样的东西。

如果你不使用部分,你可以做这样的事情:

 - (void)viewDidLoad { self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(insetLeftSide, 0, width - insetRightSide, 1)]; } 

如果您正在使用部分将每个部分中的页脚实现为方法中的一个View

 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { } 

这将允许你有一个分隔符为你的最后一个单元格,实际上不是一个分隔符是一个页脚,你可以玩它,使它看起来像一个分隔符

在iOS 8中,如果你有一个简单的样式UITableView,并添加一个页脚,最后一个分隔符不见了。 但是,您可以通过将自定义分隔视图添加到页脚来轻松地重新创build它。

这个例子完全模仿Today Widget分隔符样式

  override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { //create footer view let result = UIView() result.frame.size.height = tableView.rowHeight //recreate last cell separator, which gets hidden by footer let sepFrame = CGRectMake(15, -0.5, tableView.frame.width, 0.5); let vibrancyEffectView = UIVisualEffectView.init(effect: UIVibrancyEffect.notificationCenterVibrancyEffect()) vibrancyEffectView.frame = sepFrame vibrancyEffectView.contentView.backgroundColor = tableView.separatorColor result.addSubview(vibrancyEffectView) return result } 

如果你添加一个空的页脚,tableview会删除所有剩下的行分隔符(对于不存在的单元格),但是仍然包含最后一个单元格的分隔符:

 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { return [UIView new]; } 

适用于iOS 7.x和8.x(放在cellForRowAtIndexPath中):

(PS用您的数组数据源数replace“数据源的最大元素”)

  if (row == <max elements of your datasource>-1) { UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height-1, self.view.frame.size.width, 1)];/// change size as you need. separatorLineView.tag = 666; separatorLineView.backgroundColor = [UIColor colorWithRed: 204.0/255.0 green: 204.0/255.0 blue: 204.0/255.0 alpha:1.0]; UIView *separator = [cell.contentView viewWithTag:666]; // case of orientation changed.. if (separator.frame.size.width != cell.contentView.frame.size.width) { [[cell.contentView viewWithTag:666] removeFromSuperview]; separator = nil; } if (separator==nil) [cell.contentView addSubview:separatorLineView]; } 

这绝对有帮助。 加工。 但是从属性检查器中设置分隔符“none”。 在cellForRowAtIndexPath方法中写下面的代码

 if(indexPath.row==arrData.count-1){ UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)]; lineView.backgroundColor = [UIColor blackColor]; [cell.contentView addSubview:lineView]; } 

下面的代码适合我:

 UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 0.35)]; footerView.backgroundColor = [UIColor whiteColor]; CGRect frame = footerView.frame; frame.origin.x = 15; frame.size.width = frame.size.width - 15; UIView *blackView = [[UIView alloc] initWithFrame:frame]; [blackView setBackgroundColor:[UIColor blackColor]]; blackView.alpha = 0.25; [footerView addSubview:blackView]; tableView.tableFooterView = footerView; 

希望它也适用于你。