【问题标题】:UITableView bottom separator inset - Strange behaviour in IOS 7UITableView 底部分隔符插图 - IOS 7 中的奇怪行为
【发布时间】:2014-05-01 23:55:15
【问题描述】:

我的应用程序包含一个UITableView,它有部分页脚。当用户滚动到 tableView 的底部时,有时在最后一个单元格和 tableFooter 之间会出现一个分隔符插入。这种行为不一致。

有没有办法强制这个分隔符总是出现或从不出现?你们中有人注意到这种不一致的行为吗?对我来说似乎是一个错误。

编辑
我正在使用

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

【问题讨论】:

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

标签: ios objective-c uitableview ios7 uikit


【解决方案1】:

我找到了一些解决方法:

只要您期望这个额外的分隔符会出现(例如,当用户像您描述的那样滚动到底部时) - 添加这些代码行:

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

逻辑是这样的:第一行代码删除底部分隔符,第二行不添加。在某些情况下它对我有用,但它不是 100% 的修复。

我猜,这也可能是 Apple 的错误,因为有时底部分隔符会在他们的“提醒”应用程序中消失。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      这是一种懒惰的做法,但它仍然有效。

      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifyer"];
      
          if (indexPath.row == tableArray.count) {
               cell.separatorStyle = UITableViewCellSeparatorStyleNone;
          }
      
          return cell;
      
      }
      

      【讨论】:

      • 您不能在单个单元格上设置separatorStyle。设置cell.separatorInset = UIEdgeInsetsMake(whatever); 也无济于事。
      • @user32050560 我很确定我为 iOS 7 中的单个单元格设置了 cell.separatorInset(当我隐藏其下方的行时,通常是分组表视图中某个部分中的最后一个可见单元格)。你是你的答案吗?
      【解决方案4】:

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

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

      结果如下:

      【讨论】:

      • 我添加了更多图片以显示问题。即使您设置了backgroundColor,也会出现同样的问题。
      【解决方案5】:

      为此,您必须覆盖用作页脚的标准 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。无论如何,该错误与该方法完全无关,因为无论您是否使用它都会发生。
      • 啊,在你编辑问题之前我已经回答了。
      猜你喜欢
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      相关资源
      最近更新 更多