UITableView底部分隔线缩进 - IOS 7中奇怪的行为

8
我的应用包含一个 `UITableView`,其中有节尾。当用户滚动到tableView的底部时,有时最后一个单元格和tableFooter之间会出现分隔符插入。这种行为不一致。

enter image description here

enter image description here

有没有办法强制这个分隔符始终出现或永远不出现?你们有没有注意到这种不一致的行为?对我来说似乎像是一个 bug。

编辑
我正在使用

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UILabel *footer = [[UILabel alloc] init];

    footer.backgroundColor = [UIColor whiteColor];

    footer.font = [footer.font fontWithSize:15];
    footer.textAlignment = NSTextAlignmentCenter;

    return footer;
}

但是你可以轻松地使用相同的方法来复现这个问题。
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

enter image description here

enter image description here


你在 viewForFooter... 中的代码是什么? - Fogmeister
你已经找到解决方案了吗? - Kumar KL
我建议您跳过默认的tableView分隔符设置,将其设置为不可见: [_tableView setSeparatorColor:[UIColor clearColor]]; 然后,只需向每个单元格添加UIView子视图,该子视图的框架为CGRectMake(0,0,_tableview.frame.size.width,1),并且如果单元格行为0,则也将其隐藏。这样,它也将更加一致地与之前的iOS版本相匹配,并且您可以使用自定义分隔线图像(如果需要...)或自定义颜色或从侧面插入位置。 - Guntis Treulands
你解决这个问题了吗?我也遇到了同样的问题。听起来,用一个高度为1像素的UIView在单元格底部实现自己的分隔符是唯一可行的方法。 - JackyJohnson
5个回答

0

我找到了一些解决办法:

每当你预期可能出现这个额外的分隔符(例如,当用户像你描述的那样滚动到底部)- 添加以下代码行:

tableView.separatorStyle = UITable​View​Cell​Separator​Style​None;
tableView.separatorStyle = UITable​View​Cell​Separator​Style​Single​Line;

逻辑大致如下:第一行代码删除底部分隔符,而第二行代码则不添加。在某些情况下,这对我有用,但它并不是100%的解决方案。

我猜这可能也是苹果的一个bug,因为有时候他们的“提醒事项”应用程序中会消失底部分隔符。


0

这里是解决方案,非常简单!

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifyer"];

    cell.separatorInset = (indexPath.row == totalNumber-1) ? UIEdgeInsetsMake(0, INT16_MAX, 0, 0) : UIEdgeInsetsZero;

    return cell;

}

0

要做到这一点,您需要覆盖用作页脚的标准UIView

您可以通过覆盖委托方法-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section来实现此目的。

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *myFooterView;

    // create a view with a label and without a line at the top etc...

    return myFooterView;
}

我已经在使用viewForFooterInSection了。无论如何,这个错误与该方法完全无关,因为它会在使用或不使用该方法时发生。 - user3250560
啊,我在你编辑问题之前就回答了。 - Fogmeister

0

我不确定为什么您会得到不一致的结果。也许尝试为页脚视图添加一个背景。这是我的代码:

-(UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 33)];
    [footerView setBackgroundColor:[UIColor grayColor]];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, footerView.bounds.size.width, 20)];
    label.text = @"Footer";
    [label setTextAlignment:NSTextAlignmentCenter];

    [footerView addSubview:label];

    return footerView;
}

这里是结果:

enter image description here


我添加了更多的图片来展示问题。即使设置了 backgroundColor,仍然会发生相同的问题。 - user3250560

0

这种方法有点懒,但它仍然有效。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifyer"];

    if (indexPath.row == tableArray.count) {
         cell.separatorStyle = UITableViewCellSeparatorStyleNone;
    }

    return cell;

}

2
你不能在单个单元格上设置 separatorStyle。而且设置 cell.separatorInset = UIEdgeInsetsMake(whatever); 也没有帮助。 - user3250560
@user32050560 我非常确定我在iOS 7中为单个单元格设置了cell.separatorInset(通常是分组表视图中一个部分中最后可见的单元格,当我隐藏其下面的行时)。你确定你的答案吗? - SAHM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接