【问题标题】:Remove SeparatorInset on iOS 8 UITableView for Xcode 6 iPhone Simulator删除 Xcode 6 iPhone 模拟器的 iOS 8 UITableView 上的 SeparatorInset
【发布时间】:2014-11-03 22:38:15
【问题描述】:

我在 Xcode 6 GM 上的 iPhone 6 Simulator (iOS 8) 上的 UITableView 上发现了一个奇怪的空白。我试图从情节提要和代码中设置SeparatorInset,但空白一直存在。

以下代码适用于 iOS 7,但不适用于 iOS 8(iPhone 6 模拟器)。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
}

我在下面附上了截图:

顺便说一句,我正在使用 AutoLayout。我希望有人能告诉我一种去除TableView 上奇怪空白的方法。

【问题讨论】:

标签: ios iphone uitableview ios8 xcode6


【解决方案1】:

感谢学生用评论“试试这个 self.myTableView.layoutMargins = UIEdgeInsetsZero;”为我指出了正确的方向这行代码仅适用于 iOS 8,因为 layoutMargins 仅适用于 iOS 8。如果我在 iOS 7 上运行相同的代码,它将崩溃。

@property(nonatomic) UIEdgeInsets layoutMargins
Description   The default spacing to use when laying out content in the view.
Availability  iOS (8.0 and later)
Declared In   UIView.h
Reference UIView Class Reference

下面是解决这个奇怪空白的正确答案,方法是将tableview layoutMarginscell layoutMargins 设置为UIEdgeInsetsZero(如果存在)(对于 iOS 8)。它也不会在 iOS 7 上崩溃。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

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

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

请看下面的截图:-

【讨论】:

  • 感谢您也使它与 iOS7 兼容。在 iOS8 上运行良好
  • 我发现,为了让它适用于 iOS 8,我必须在 ViewDidAppear 中重新加载表格视图。
  • 我从这个博客datacalculation.blogspot.in/2014/10/…得到了一个工作参考
  • 有效。如果有使用外观的解决方案并将此代码从表实现中删除,我会非常感兴趣
【解决方案2】:

尝试创建一个 UITableViewCell 类类别并添加这个 getter

- (UIEdgeInsets)layoutMargins {
    return UIEdgeInsetsZero;
}

在iOS7中不会调用这个,因为SDK中没有这个属性,不会导致崩溃; 在 iOS8 中,每次使用单元格时都会调用它

对我有用

【讨论】:

  • 这对我很有用。如果您有自定义单元格,这就是您需要添加的全部内容。
  • 这是我实现自定义单元格时唯一有效的解决方案。
  • 这个真的需要更多的支持。在我的情况下完美运行。
  • 简单、优雅的全球解决方案!无需在整个项目中为每个 UITableView 添加 layoutMargin 设置 - 只需添加一个类别,一切都会好的。谢谢!
【解决方案3】:

我的解决方案只有三行代码:

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)row{
    //
    // ... your code ...
    //
    if ([cell respondsToSelector:@selector(preservesSuperviewLayoutMargins)]){
        cell.layoutMargins = UIEdgeInsetsZero;
        cell.preservesSuperviewLayoutMargins = false;
    }
    return cell;
}

【讨论】:

    【解决方案4】:

    IOS8引入了一个名为Configuring Content Margins的新概念,同时引入了一个名为layoutMargins的新属性,该属性的详细信息请参考Apple文档。 layoutMargins 的类型为 UIEdgeInsets ,默认值为 {8,8,8,8} 。 IOS8中删除TableView的分隔线,除了设置tableView.seperatorInset = UIEdgeInsetsZero外,还需要as:

    先定义宏

    #define isIOS8SystemVersion (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
    

    在 UITableViewDelegate 方法中添加:

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
             static NSString *reuseId = @"cellReuseID" ;
             UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
             if(!cell){
                 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseId];     
             if(isIOS8SystemVersion){   
                 cell.layoutMargins = UIEdgeInsetsZero;
                 cell.preservesSuperviewLayoutMargins =NO ;
             }
    
        }
    

    这样做会删除分隔线。你也可以这样做:

        UITableView *tableView = [[UITableView alloc] init];
        if(isIOS8SystemVersion){
             tableView.layoutMargins = UIEdgeInsetsZero ;
        }
    

    并在 UITableViewDelegate 方法中添加:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         static NSString *reuseId = @"cellReuseID" ;
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
         if(!cell){
             cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseId];     
         if(isIOS8SystemVersion){   
             cell.layoutMargins = UIEdgeInsetsZero;
         }
    }
    

    【讨论】:

    • 对我来说,cell.preservesSuperviewLayoutMargins = NO;是关键。
    【解决方案5】:

    在我的 Xcode 6.2 中,除了 Will Q 的回答之外,我还必须转到 Main.storyboard > 选择 UITableViewCell > Attributes Inspector。将分隔符下拉列表从默认插图更改为 自定义插图。将左插图从 15 更改为 0。

    【讨论】:

      【解决方案6】:

      iOS 7 和 iOS 8 的解决方法

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {    
          cell.separatorInset = UIEdgeInsetsMake(0.0f, cell.frame.size.width, 0.0f, 0.0f); 
      }
      

      【讨论】:

      • 这应该是正确的答案。又短又甜! :) 100% 有效
      • 其他答案都不适合我,同意@emotality
      • 同意这是正确的答案。在 iOS 9 中也可以完美运行。其他答案将单元格之间的分隔线与部分末尾的分隔线混合在一起
      【解决方案7】:

      在 iOS8 中,您必须在行和表级别设置插入 both

      cellForRowAtIndexPath 中的行级别:

      if ([cell respondsToSelector:@selector(preservesSuperviewLayoutMargins)]){
          cell.layoutMargins = UIEdgeInsetsZero;
          cell.preservesSuperviewLayoutMargins = false;
      }
      

      viewDidLoad 中的表级别:

      [tableReference setSeparatorInset:UIEdgeInsetsZero];
      

      之后,清理您的项目是个好主意。在某些情况下,我注意到这些更改并没有直接引入模拟器的可执行应用程序中。

      【讨论】:

        【解决方案8】:

        适用于 iOS 8

        尝试在cellForRowAtIndexPath方法中设置cell.layoutMargins = UIEdgeInsetsZero;

        【讨论】:

        • 我已经尝试过了,就像我上面提到的,这种方法只适用于 iOS 7。
        • 试试这个 self.myTableView.layoutMargins = UIEdgeInsetsZero;
        【解决方案9】:

        如果要删除白线,但保持分隔符插入不变,只需将 cell.backgroundColor 设置为 tableView 的背景颜色。仅设置 cell.contentView.backgroundColor 不会使问题消失。

        【讨论】:

          【解决方案10】:

          我尝试了很多方法,但没有一种方法对我有用。

          【讨论】:

            【解决方案11】:

            为 iPhone 6 和 Plus 添加启动图像。在添加图像之前,手机在缩放模式下运行,即旧应用程序被缩放以适应新的屏幕尺寸。这可能是造成线条的原因。 新图像是 Retina HD 5.5 (iPhone6Plus) 1242x2208 和 Retina HD 4.7 (iPhone6) 750x1334。

            【讨论】:

            • 实际上,从几周前开始,我正在使用 XCode 6 Beta 通过实施自动布局来重新开发应用程序。因此,它实际上是一个全新的应用程序。无论如何,我已经找到了上面的答案。谢谢。
            【解决方案12】:

            iOS 8 UITableView separator inset 0 not working

            基本上,你需要同时设置 cell.layoutMargin 以及 tableView 的 layoutMargin。 YMMV,但我必须在 layoutSubviews 中设置表格视图才能正常工作!

            【讨论】:

              【解决方案13】:

              要使 iOS7 和 iOS8 的 UITableView 分隔符插入为零,而不是更改代码,而是通过更改 View->Mode->Aspect Fill 在 UITableView 的 xib 中进行更改。

              【讨论】:

              • 能否请您详细说明您的答案,添加更多关于您提供的解决方案的描述?
              • UITableView 上的内容模式标志用于确定视图在其边界发生变化时如何布置其内容。 AspectFill 会将 tableViewCell 填充到其中,这将填充整个视图,而不是为 IOS8 添加的边距。有关 ContentMode 的更多信息,请参阅 Apple 文档 developer.apple.com/library/ios/#documentation/uikit/reference/…
              【解决方案14】:

              我有静态 UITableView 并且想将单元格分隔符的边距移动到左边缘。

              感谢以上答案,这是我的解决方案

              override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
                  // needed to shift the margin a specific cell to the left edge
                  if indexPath.section == 3 && indexPath.row == 0 {
                      cell.layoutMargins = UIEdgeInsetsZero
                      cell.preservesSuperviewLayoutMargins = false
                      cell.separatorInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)
                  }
              }
              

              【讨论】:

              • 仅适用于 cell.separatorInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0),谢谢
              【解决方案15】:

              只需添加以下内容:

              tableView.separatorStyle = .none
              

              【讨论】:

                猜你喜欢
                • 2014-11-05
                • 2014-12-27
                • 2014-11-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-10-15
                • 2014-11-08
                相关资源
                最近更新 更多