【问题标题】:Is there a way to make UITableView cells in iOS 7 not have a line break in the separator?有没有办法让 iOS 7 中的 UITableView 单元格在分隔符中没有换行符?
【发布时间】:2014-12-01 07:51:17
【问题描述】:

我注意到在 iOS 7 中,UITableViewCells 在单元格的分隔符中有一个换行符,而 iOS 6 没有。有没有办法摆脱这个换行符?将分隔符更改为无,然后使用分隔符的颜色制作 UIViews 仍然会导致出现白色分隔符。

【问题讨论】:

    标签: uitableview ios7


    【解决方案1】:

    对于 iOS7:

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

    对于 iOS8:

    首先配置你的表格视图如下:

    if ([self.tableView respondsToSelector:@selector(layoutMargins)]) {
        self.tableView.layoutMargins = UIEdgeInsetsZero;
    }
    

    然后在你的 cellForRowAtIndexPath: 方法中,配置单元格如下:

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

    注意:同时包含 layoutMarginsseparatorInset,以支持两个 iOS 版本

    【讨论】:

    • 这一行应该用什么方法写?对我不起作用。
    • 在 ViewDidLoad 方法中添加这个。有用。如果你想让你的tableviews iOS7ish,你也可以改变插图。
    • 更新你的答案
    • 我发现我需要 both 上面为 iOS8 提供的 iOS7 和 iOS8 部分。
    • 更新:如果您在viewDidLayoutSubviews 中设置layoutMargins,iOS 8 修复程序有效。如果您将其设置为viewDidLoad 或类似名称,则它不起作用。我已经编辑了答案,以便清楚。
    【解决方案2】:

    您还可以从情节提要中设置“Separator Inset”:

    【讨论】:

    • 对于那些在 UIView 或 UIViewController 父级中有 UITableView 的人,你不会看到上面的图像,但是如果你检查里面的 UITableView,你会看到“Separator Insets”设置为“Default”你的.xib。选择“自定义”并将左侧或右侧更改为 0。换句话说,不要浪费时间尝试所有其他东西!通过 IB 设置插图..
    • 这听起来不错,但对我不起作用。我创建了一个新项目,拖入一个 TVC,将其设为根 VC,并将左插图更改为 0。我运行了模拟器,分隔符仍然是插图。知道为什么吗? nobre 的解决方案(如下)对我有用。
    • 谢谢!你为我节省了很多时间。
    【解决方案3】:

    如果你想支持旧版本的iOS,你应该在调用它之前检查这个方法的可用性:

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

    【讨论】:

    • 这更安全,如果你不是以 ios6 为目标
    • 如何在 iOS6 中获得这个?
    【解决方案4】:

    想通了。

    [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    

    【讨论】:

    • 这对我也有用。顺便说一句,我不明白为什么分离器会出现这些中断。在我的应用程序中的一个表视图中,相同的分隔线是双的,而其他的是单行的。为什么 - 不知道。
    • 它确实适用于空白表视图。但是我的自定义可重复使用的单元格分隔符仍然是默认设置
    【解决方案5】:

    所选答案对我不起作用。但这是肯定的,它适用于 ios 2.0:

       [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    

    【讨论】:

    • 只有当你想要 no 分隔符时才好。
    • 不,如果你根本不想要任何风格,那是他想要的。
    • 这没有回答问题。
    【解决方案6】:

    斯威夫特版

    iOS 在单元格和表格视图上引入了 layoutMargins 属性。

    此属性在 iOS 7.0 中不可用,因此您需要确保在分配之前检查!

    但是,Apple 已向您的单元格添加了一个 ** 属性,这将阻止它继承您的表格视图的边距设置。这样,您的单元格可以独立于表格视图配置自己的边距。将其视为一种覆盖。

    此属性称为 preservesSuperviewLayoutMargins,将其设置为 NO 可以让您使用自己单元格的 layoutMargin 设置覆盖 Table View 的 layoutMargin 设置。它既节省时间(您不必修改表格视图的设置),而且更简洁。详细解释请参考 Mike Abdullah 的回答。

    注意:正如 Mike Abdullah 的回答中所表达的那样,这是正确的、不那么混乱的实现;设置单元格的 preservesSuperviewLayoutMargins=NO 将确保您的表格视图不会覆盖单元格设置。

    第一步 - 设置单元格边距:

    /*
        Tells the delegate the table view is about to draw a cell for a particular row.
    */
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
        forRowAtIndexPath indexPath: NSIndexPath)
    {
        // Remove seperator inset
        if cell.respondsToSelector("setSeparatorInset:") {
            cell.separatorInset = UIEdgeInsetsZero
        }
    
        // Prevent the cell from inheriting the Table View's margin settings
        if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
            cell.preservesSuperviewLayoutMargins = false
        }
    
        // Explictly set your cell's layout margins
        if cell.respondsToSelector("setLayoutMargins:") {
            cell.layoutMargins = UIEdgeInsetsZero
        }
    }
    

    将单元格上的 preservesSuperviewLayoutMargins 属性设置为 NO 应该防止表格视图覆盖单元格边距。在某些情况下,它似乎无法正常工作。

    第二步 - 只有当一切都失败时,你才可以暴力破解你的表格视图边距:

    /*
        Called to notify the view controller that its view has just laid out its subviews.
    */
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
        // Force your tableview margins (this may be a bad idea)
        if self.tableView.respondsToSelector("setSeparatorInset:") {
            self.tableView.separatorInset = UIEdgeInsetsZero
        }
    
        if self.tableView.respondsToSelector("setLayoutMargins:") {
            self.tableView.layoutMargins = UIEdgeInsetsZero
        }
    }
    

    ...你去吧!这应该适用于 iOS 8 和 iOS 7。

    注意:使用 iOS 8.1 和 7.1 进行测试,在我的情况下,我只需要使用本说明的第一步。

    只有在渲染单元格下方有未填充的单元格时,才需要第二步,即。如果表大于表模型中的行数。不做第二步会导致不同的分隔符偏移。

    【讨论】:

      【解决方案7】:

      对于应用程序范围内的永久解决方案,请在 application:didFinishLaunching 中调用它。任何 tableview 都将被“修复”

      if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        [[UITableView appearance]setSeparatorInset:UIEdgeInsetsZero];
      }
      

      【讨论】:

        【解决方案8】:

        在 iOS 8 中,根据 Seth McFarlane 的帖子 here,您需要开始处理 layoutMargins。以下为我做了:

        1. 在 ApplicationDidFinishLaunching 中,运行以下命令:

          if ([UITableViewCell instancesRespondToSelector:@selector(setLayoutMargins:)]) {
              [[UITableViewCell appearance]setLayoutMargins:UIEdgeInsetsZero];
          }
          
        2. 在 UITableView 上创建如下分类:

          @interface UITableView(WJAdditions)
          -(void) wjZeroSeparatorInset;
          @end
          
          @implementation UITableView (WJAdditions)
          -(void) wjZeroSeparatorInset {
          #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
            if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
              self.layoutMargins = UIEdgeInsetsZero;
            }
          #endif
          #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
             if ([self respondsToSelector: @selector(setSeparatorInset:)])
                self.separatorInset = UIEdgeInsetsZero;
             }
          #endif
          }
          @end
          
        3. 在你的 UITableViews 中,初始化后不久,调用

          [self wjZeroSeparatorInset];
          
        4. 在您的 UITableViewControllers 中,您需要以下内容:

          -(void) viewWillLayoutSubviews {
            [super viewWillLayoutSubviews];
            UITableView* tv = self.tableView;
            if ([tv respondsToSelector:@selector(setLayoutMargins:)]) {
              [tv setLayoutMargins:UIEdgeInsetsZero];
            }
          }
          

        【讨论】:

          【解决方案9】:

          在 iOS 8 中,似乎存在一个小错误。分隔符插入的自定义值不会应用于其中包含文本的单元格。我尝试从界面构建器和代码中设置它,但都没有成功。我也提交了错误报告。

          与此同时,我有一个小解决方法来实现这一点。我只是在单元格上画线。创建UITableViewCell 的子类并覆盖drawRect 方法。另外不要忘记将UITableView 的分隔符属性设置为无。这是代码。它在 Swift 中,但由于它基本上是 C,因此您也可以在 Objective-C 中使用相同的东西。

          override func drawRect(rect: CGRect) {
              super.drawRect(rect)
          
              let context = UIGraphicsGetCurrentContext()
              CGContextSetStrokeColorWithColor(context, UITableView().separatorColor.CGColor) // seperator color
              CGContextSetLineWidth(context, 2) // separator width
          
              CGContextMoveToPoint(context, 0, self.bounds.size.height)
              CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height)
              CGContextStrokePath(context)
          }
          

          【讨论】:

          • 谢谢。 CGContextSetLineWidth(context, 1) 更接近默认值,以防有人想要更“香草”的外观
          【解决方案10】:

          Reset separatorInset 为我解决了这个问题:

          if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
              [self.tableView setSeparatorInset:self.tableView.separatorInset];
          }
          

          【讨论】:

          • 如果您不是针对 ios6,这会更安全。
          【解决方案11】:

          解决办法:

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

          【讨论】:

            【解决方案12】:

            注意将 tableView 的 separatorInset 设置为 UIEdgeInsetsZero 似乎同时破坏了节标题的左边距! (至少在 iOS 7.1 上是这样)

            如果你想用tableView:titleForHeaderInSection:显示节标题,并且仍然得到UITableViewCells之间不换行的期望效果,你必须直接在单元格上设置separatorInset而不是tableView!

            - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
            {
                // ...
                [cell setSeparatorInset:UIEdgeInsetsZero];
            

            【讨论】:

              【解决方案13】:

              对于 iOS8+,这对我有用,

              tableView.layoutMargins = UIEdgeInsetsZero
              

              cellForRowAtIndexPath方法中:

              cell.layoutMargins = UIEdgeInsetsZero;
              

              【讨论】:

                【解决方案14】:
                - (void)viewDidLoad
                {
                    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
                
                    [super viewDidLoad];
                
                    // Additional setup
                }
                

                注意:setSeparatorStyle 行必须在 super 调用之上。

                【讨论】:

                • 这没有回答问题。
                【解决方案15】:
                @implementation UITableView (HGApperance) 
                -(void)setSeparatorXMargin:(CGFloat)margin{
                    // iOS 7
                    if ([self respondsToSelector:@selector(setSeparatorInset:)]) {
                        [self setSeparatorInset:UIEdgeInsetsZero];
                    }
                
                    if ([self respondsToSelector:@selector(layoutMargins)]) {
                        self.layoutMargins = UIEdgeInsetsZero;
                    }
                }
                @end
                

                对于 TableviewCell,Willam 的 answer 非常完美。

                【讨论】:

                  【解决方案16】:

                  至 iOS8 及更高版本

                  如果你发现所有的解决方案都不像我这样,我想你可能已经使用了UITableViewCell的子类并重写了layoutSubviews方法,如果你满足两个条件,请继续阅读。

                  一步一步做或检查。

                  1、分配并初始化UITableView后,设置其layoutMargins属性。

                  if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
                      _tableView.layoutMargins = UIEdgeInsetsZero ;
                  }
                  

                  2、在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中,在alloc并初始化你的自定义UITableViewCell之后,设置它的layoutMargins属性。

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

                  3、最重要的部分来了。如果你重写了UITableViewCell的- (void)layoutSubviews方法,别忘了调用它的super

                  - (void)layoutSubviews
                  {
                      [super layoutSubviews] ;
                  
                      // layout the subviews
                  }
                  

                  【讨论】:

                    【解决方案17】:

                    Xamarin.iOS 版本

                    Luis E. PradoKing-Wizard 的帖子的帮助下,我在 iOS 8.4 上的 Xamarin.iOS 中将 UITableView 分隔符扩展了整个宽度。我必须同时设置 SeparatorInset 和 LayoutMargins 才能让它工作。由于我的目标是 iOS 8 及更高版本,因此我没有为较低的 iOS 版本编写检查。

                    我编写了以下两个 C# 扩展方法来在 UITableView 或 UITableViewCell 级别设置 UITableView 分隔符全宽。您可以使用其中一种或两种方法,具体取决于所需的效果(请参阅下面的说明)。

                    UITableView

                    设置 UITableView 属性以更改出现在空单元格之后的分隔符。这个 SetLayoutMarginsZero 方法可以从相关的 ViewController 的 ViewDidLoad 方法中调用。

                    public static void SetLayoutMarginsZero(this UITableView tableView)
                    {
                        tableView.SeparatorInset = UIEdgeInsets.Zero;
                        tableView.LayoutMargins = UIEdgeInsets.Zero;
                    }
                    

                    UITableViewCell

                    设置 UITableViewCell 属性以更改填充单元格之后出现的分隔符。这个 SetLayoutMarginsZero 方法可以从 TableViewSource GetCell 方法中调用。

                    public static void SetLayoutMarginsZero(this UITableViewCell tableViewCell)
                    {
                        tableViewCell.SeparatorInset = UIEdgeInsets.Zero;
                        tableViewCell.LayoutMargins = UIEdgeInsets.Zero;
                        tableViewCell.PreservesSuperviewLayoutMargins = false;
                    }
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2010-10-29
                      • 1970-01-01
                      • 1970-01-01
                      • 2019-06-28
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多