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

时间:2013-11-04 12:14:13

标签: ios objective-c uitableview ios7

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

基本上这就是我所追求的目标。

enter image description here 这是来自原生音乐应用程序,这使我认为应该可以通过UITableView实现,他们不会使用一些私有API用于细胞分离器,对吗?

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

  1. 当选择/突出显示单元格分隔符时,上一个单元格的分隔符会自动隐藏(请参阅第二个屏幕截图中的“你必须疯狂“选中”。这是 我希望UITableView处理的东西,而不是自己,如果我使用单像素线 (当细胞分离器不延伸时,这尤其方便 到表格视图边缘的方式,分隔符和选定的单元格背景看起来不太好。)
  2. 我希望让我的单元格尽可能保持平坦以进行滚动表现。
  3. UITableView参考中还有一些东西让我觉得有一种简单的方法可以得到我想要的东西:

      

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

    有人知道如何使用普通样式表底部绘制的这些“额外”分隔符吗?因为这正是我所需要的。 我认为将separatorInset分配给UITableView,而不是UITableViewCell可以做到这一点,但事实并非如此,最后一个单元格仍然缺少其分隔符。

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

14 个答案:

答案 0 :(得分:16)

这为我添加一个分隔符到最后一个单元格完美无缺。玩得开心!

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

答案 1 :(得分:10)

将headerView或footerView添加到TableView时,最后一个分隔线将消失。

下面的示例将让您在最后一个单元格上显示分隔符。你必须要实现更多的是在选择单元格后使这个分隔符消失,所以行为与其他单元格中的行为相同。

对于Swift 4.0

(bmd-)form-group

答案 2 :(得分:8)

我发现了一些对我有用的东西。简而言之:为您的UITableView提供tableFooterView并将其框架高度设置为0 。这使得一个实际的分隔符显示,带有正确的插入。

更多详细信息:如果您正在使用故事板,请将UIView拖到表视图的底部(在左侧的树视图中),以便它显示在同一层级的表视图单元格下方。这会创建一个tableFooterView。当然,这也可以通过编程方式完成。

然后,在UITableViewController的实现中:

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

如果这对你有用,请告诉我!如果您使用tableHeaderView,它也可能适用于第一个分隔符,但我还没有尝试过。

答案 3 :(得分:3)

如果您不使用部分,可以执行以下操作:

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

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

中的单点视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
}

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

这将允许您为最后一个单元格设置一个分隔符,该分隔符实际上不是分隔符,是一个可以与它一起玩并使其看起来像分隔符的页脚

答案 4 :(得分:2)

这将完全符合您的要求..即使该行将占用单元格的整个宽度:

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

中的

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

答案 5 :(得分:2)

我遇到了类似的问题并找到了解决方法。 Apple会隐藏最后一个uitableviewcell分隔符,然后在您选择单元格或者在该单元格上调用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;
    }
}

像这样。

答案 6 :(得分:2)

在iOS 8中,如果你有一个普通的UITableView风格,并添加一个页脚,那么最后一个分隔符就不见了。但是,您可以通过向页脚添加自定义分隔符视图来轻松地重新创建它。

此示例完全模仿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
}

答案 7 :(得分:2)

所以这是 Swift 4 中的一个超级简单的解决方案:

override func viewDidLoad(){}内,我只是实现了这行代码:

    self.tableView.tableFooterView = UIView(frame: .zero)

因此,它确保只有最后一个单元格插入分隔符。

这对我来说非常合适,希望它也适合你!

答案 8 :(得分:1)

如果您添加一个空页脚,那么tableview将删除所有剩余的行分隔符(对于不存在的单元格),但仍将包含最后一个单元格的分隔符:

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

答案 9 :(得分:1)

Swift 3

我发现了这个老问题,所以我在Swift中尝试了同样的事情:

    tableView.tableFooterView = UIView()
    tableView.tableFooterView?.height = 0 // Maybe not necessary

但它导致另一个问题(在我的情况下)。所以我解决了不同的问题。我在本节末尾添加了一个额外的单元格,空白,高度等于零:

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return previousNumberOfRows + 1
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
      return indexPath.row == previousNumberOfRows ? 0 : previousCellHeight
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      if indexPath.row == items.count {
        return UITableViewCell()
    } else {
       previousCellInitialization()
    }

这是一个不太简洁的解决方案,但在更多情况下,如果你有多个部分,现有的页脚视图...

答案 10 :(得分:0)

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

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

 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];
 }

答案 11 :(得分:0)

这绝对有帮助。工作。 但设置分隔符&#34;无&#34;来自属性检查员。 在 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];
}

答案 12 :(得分:0)

以下代码对我有用:

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;

希望它也适合你。

答案 13 :(得分:0)

我使用“分组”的表格视图样式添加了这一行:

enter image description here

此外,它还避免了滚动时最后一条分隔线粘贴在屏幕上!