【问题标题】:Hide separator line on one UITableViewCell在一个 UITableViewCell 上隐藏分隔线
【发布时间】:2012-01-23 14:37:35
【问题描述】:

我正在自定义 UITableView。我想隐藏 last 单元格上的分隔线...我可以这样做吗?

我知道我可以做到tableView.separatorStyle = UITableViewCellStyle.None 但这会影响 所有 tableView 的单元格。我希望它只影响我的最后一个单元格。

【问题讨论】:

标签: ios objective-c uitableview cocoa-touch separator


【解决方案1】:

viewDidLoad中,添加这一行:

self.tableView.separatorColor = [UIColor clearColor];

cellForRowAtIndexPath

适用于 iOS 较低版本

if(indexPath.row != self.newCarArray.count-1){
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
    line.backgroundColor = [UIColor redColor];
    [cell addSubview:line];
}

适用于 iOS 7 更高版本(包括 iOS 8)

if (indexPath.row == self.newCarArray.count-1) {
    cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
}

【讨论】:

  • 这将适用于 iOS7 和 iOS8。它有效地将分离器压缩到零。 cell.separatorInset = UIEdgeInsetsMake(0, CGRectGetWidth(cell.bounds)/2.0, 0, CGRectGetWidth(cell.bounds)/2.0)
  • 一个提醒:当你的iDevice是iPad并且cell使用AutoLayout时,“cell.bounds.size.width”返回的值可能不等于实际cell的宽度。所以我总是使用“tableView.frame.size.width”而不是“cell.bounds.size.width”。
  • 请注意:您应该使用[cell.contentView addSubview:line] 而不是[cell addSubview:line]
  • change cell.separatorInset left inset 也会改变单元格内容的左插图。不仅仅是分隔线。来自苹果文档:“您可以使用此属性在当前单元格的内容与表格的左右边缘之间添加空间。正插入值将单元格内容和单元格分隔符向内移动并远离表格边缘。”
  • 坏主意。你不应该在cellForRowAtIndexPath 的单元格中添加子视图。请记住,单元格被重复使用。每次重用此单元格时,您将添加另一个分隔线视图。在大型列表中,这可能会影响滚动性能。而且这不是正确的做法。
【解决方案2】:

试试下面的代码,或许能帮你解决问题

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

   NSString* reuseIdentifier = @"Contact Cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (indexPath.row != 10) {//Specify the cell number
        cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgWithLine.png"]];

} else {
        cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgWithOutLine.png"]];

}

    }

    return cell;
}

【讨论】:

    【解决方案3】:

    如果您不想自己绘制分隔符,请使用:

      // Hide the cell separator by moving it to the far right
      cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
    

    不过,此 API 仅从 iOS 7 开始可用。

    【讨论】:

    • separatorInset 似乎插入了单元格内容和分隔符,需要另一个 hack 来弥补:cell.IndentationWidth = -10000;
    • 更好的方法是将separatorInset设置为上下左右为0,单元格宽度为右侧:cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);这样可以避免调整任何其他单元格属性。
    • 如果您使用插入的单元格边界宽度,您可能需要在界面旋转时重新计算。
    • 请注意,如果您执行此操作的单元格被重复用于绘制另一个您不打算隐藏分隔符的单元格,则分隔符也会从中消失。
    • UIEdgeInsetsMake(0, 10000, 0, 0);工作-> UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);没有。猜想这是因为我有一个讨厌的习惯,即在 xibs 和 styoryboards 中保留 3.5" 大小的 vcs,4"+ 设备上的工件导致 375-320px 部分 = 55px 仍然存在。 (用尤达的声音)而且非常丑陋!
    【解决方案4】:

    UITableViewDataSourcecellForRowAtIndexPath方法中

    斯威夫特:

    if indexPath.row == {your row number} {
        cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
    }
    

    或:

    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, UIScreen.main.bounds.width)
    

    默认保证金:

    cell.separatorInset = UIEdgeInsetsMake(0, tCell.layoutMargins.left, 0, 0)
    

    端到端显示分隔符

    cell.separatorInset = .zero
    

    目标-C:

    if (indexPath.row == {your row number}) {
        cell.separatorInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, CGFLOAT_MAX);
    }
    

    【讨论】:

    • 不适用于分组的UITableView,而接受的答案可以。
    • 这不适用于 iOS9,self.tableView.separatorColor = [UIColor clearColor]; 已修复。
    • 这是一个完整的 hack,但在 iOS 9 中有效的是: cell.layoutMargins = UIEdgeInsetsZero; cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 9999)
    • 这是它在 iOS 8+ 中为我工作的方式:cell.separatorInset = UIEdgeInsetsMake(0.f, 0.f, 0.f, cell.bounds.size.width-cell.layoutMargins.left); 如果我不减去 cell.layoutMargins.left 值,则分隔线从左边框向左绘制边距(如果有的话)。
    • 这会将任何textLabel 推到屏幕外。
    【解决方案5】:

    在 iOS 7 中,UITableView 分组样式单元格分隔符看起来有点不同。有点像这样:

    我尝试了 Kemenaran 的 answer 这样做:

    cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
    

    但这似乎对我不起作用。我不确定为什么。所以我决定用Hiren的answer,但是用UIView而不是UIImageView,并以iOS 7风格画线:

    UIColor iOS7LineColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];
    
    //First cell in a section
    if (indexPath.row == 0) {
    
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
        line.backgroundColor = iOS7LineColor;
        [cell addSubview:line];
        [cell bringSubviewToFront:line];
    
    } else if (indexPath.row == [self.tableViewCellSubtitles count] - 1) {
    
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(21, 0, self.view.frame.size.width, 1)];
        line.backgroundColor = iOS7LineColor;
        [cell addSubview:line];
        [cell bringSubviewToFront:line];
    
        UIView *lineBottom = [[UIView alloc] initWithFrame:CGRectMake(0, 43, self.view.frame.size.width, 1)];
        lineBottom.backgroundColor = iOS7LineColor;
        [cell addSubview:lineBottom];
        [cell bringSubviewToFront:lineBottom];
    
    } else {
    
        //Last cell in the table view
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(21, 0, self.view.frame.size.width, 1)];
        line.backgroundColor = iOS7LineColor;
        [cell addSubview:line];
        [cell bringSubviewToFront:line];
    }
    

    如果您使用它,请确保在第二个 if 语句中插入正确的表格视图高度。我希望这对某人有用。

    【讨论】:

      【解决方案6】:

      iphone 的宽度是 320 。所以将左值和右值放在 Cell 属性中为 separatorInset 超过 320 的一半。

      【讨论】:

      • 如果应用程序适用于 ipad 或 iPhone 6 或 6+,则无法正常工作。
      【解决方案7】:
      - (void)viewDidLoad {
          [super viewDidLoad];
          // Do any additional setup after loading the view.
          [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
      }
      

      【讨论】:

      • 完全隐藏整个表格的单元格分隔符。
      【解决方案8】:

      跟进Hiren的回答。

      ViewDidLoad 和以下行:

      self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
      

      或者,如果您使用的是 XIB 或 Storyboard,请将“separator”更改为“none”:

      并在 CellForRowAtIndexPath 中添加:

      CGFloat separatorInset; // Separator x position 
      CGFloat separatorHeight; 
      CGFloat separatorWidth; 
      CGFloat separatorY; 
      UIImageView *separator;
      UIColor *separatorBGColor;
      
      separatorY      = cell.frame.size.height;
      separatorHeight = (1.0 / [UIScreen mainScreen].scale);  // This assures you to have a 1px line height whatever the screen resolution
      separatorWidth  = cell.frame.size.width;
      separatorInset  = 15.0f;
      separatorBGColor  = [UIColor colorWithRed: 204.0/255.0 green: 204.0/255.0 blue: 204.0/255.0 alpha:1.0];
      
      separator = [[UIImageView alloc] initWithFrame:CGRectMake(separatorInset, separatorY, separatorWidth,separatorHeight)];
      separator.backgroundColor = separatorBGColor;
      [cell addSubView: separator];
      

      这是一个结果示例,其中我显示了一个带有动态单元格的表格视图(但只有一个带有内容的表格视图)。结果是只有那个有分隔符,而不是所有的“虚拟”tableview 都会自动添加以填满屏幕。

      希望这会有所帮助。

      编辑:对于那些不总是阅读 cmets 的人来说,实际上有一个更好的方法可以用几行代码来做到这一点:

      override func viewDidLoad() {
          super.viewDidLoad()
          tableView.tableFooterView = UIView()
      }
      

      【讨论】:

      • 我认为,隐藏分隔线,这是正确的做法。 self.tableView.separatorStyle = .none
      【解决方案9】:

      适用于 iOS 7 和 8 的更好解决方案

      -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
      {
          DLog(@"");
          if (cell && indexPath.row == 0 && indexPath.section == 0) {
      
              DLog(@"cell.bounds.size.width %f", cell.bounds.size.width);
              cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.0f);
          }
      }
      

      如果您的应用是可旋转的 - 使用 3000.0f 作为左插入常量或动态计算它。 如果您尝试设置右插图,则在 iOS 8 的单元格左侧有可见的分隔符部分。

      【讨论】:

      • 当你可以做这样的事情时为什么要使用随机数:MAX([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);以确保它总是消失
      【解决方案10】:
        if([_data count] == 0 ){
             [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];//  [self tableView].=YES;
          } else {
            [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];////    [self tableView].hidden=NO;
          }
      

      【讨论】:

        【解决方案11】:
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
               NSString *cellId = @"cell";
               UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
               NSInteger lastRowIndexInSection = [tableView numberOfRowsInSection:indexPath.section] - 1;
        
               if (row == lastRowIndexInSection) {
                      CGFloat halfWidthOfCell = cell.frame.size.width / 2;
                      cell.separatorInset = UIEdgeInsetsMake(0, halfWidthOfCell, 0, halfWidthOfCell);
               }
        }
        

        【讨论】:

          【解决方案12】:

          您必须采用自定义单元格并添加标签并设置约束,例如标签应覆盖整个单元格区域。 并在构造函数中写入以下行。

          - (void)awakeFromNib {
              // Initialization code
              self.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
              //self.layoutMargins = UIEdgeInsetsZero;
              [self setBackgroundColor:[UIColor clearColor]];
              [self setSelectionStyle:UITableViewCellSelectionStyleNone];
          }
          

          同时设置 UITableView 布局边距如下

          tblSignup.layoutMargins = UIEdgeInsetsZero;
          

          【讨论】:

            【解决方案13】:

            如果接受的答案不起作用,您可以试试这个:

            - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
                return 0.01f; }
            

            太棒了;)

            【讨论】:

              【解决方案14】:

              实现这一点的最佳方法是关闭默认行分隔符,子类UITableViewCell 并添加自定义行分隔符作为contentView 的子视图 - 请参阅下面的自定义单元格,用于呈现类型对象SNStock 有两个字符串属性,tickername

              import UIKit
              
              private let kSNStockCellCellHeight: CGFloat = 65.0
              private let kSNStockCellCellLineSeparatorHorizontalPaddingRatio: CGFloat = 0.03
              private let kSNStockCellCellLineSeparatorBackgroundColorAlpha: CGFloat = 0.3
              private let kSNStockCellCellLineSeparatorHeight: CGFloat = 1
              
              class SNStockCell: UITableViewCell {
              
                private let primaryTextColor: UIColor
                private let secondaryTextColor: UIColor
              
                private let customLineSeparatorView: UIView
              
                var showsCustomLineSeparator: Bool {
                  get {
                    return !customLineSeparatorView.hidden
                  }
                  set(showsCustomLineSeparator) {
                    customLineSeparatorView.hidden = !showsCustomLineSeparator
                  }
                }
              
                var customLineSeparatorColor: UIColor? {
                 get {
                   return customLineSeparatorView.backgroundColor
                 }
                 set(customLineSeparatorColor) {
                   customLineSeparatorView.backgroundColor = customLineSeparatorColor?.colorWithAlphaComponent(kSNStockCellCellLineSeparatorBackgroundColorAlpha)
                  }
                }
              
                required init(coder aDecoder: NSCoder) {
                  fatalError("init(coder:) has not been implemented")
                }
              
                init(reuseIdentifier: String, primaryTextColor: UIColor, secondaryTextColor: UIColor) {
                  self.primaryTextColor = primaryTextColor
                  self.secondaryTextColor = secondaryTextColor
                  self.customLineSeparatorView = UIView(frame:CGRectZero)
                  super.init(style: UITableViewCellStyle.Subtitle, reuseIdentifier:reuseIdentifier)
                  selectionStyle = UITableViewCellSelectionStyle.None
                  backgroundColor = UIColor.clearColor()
              
                  contentView.addSubview(customLineSeparatorView)
                  customLineSeparatorView.hidden = true
                }
              
                override func prepareForReuse() {
                  super.prepareForReuse()
                  self.showsCustomLineSeparator = false
                }
              
                // MARK: Layout
              
                override func layoutSubviews() {
                  super.layoutSubviews()
                  layoutCustomLineSeparator()
                }
              
                private func layoutCustomLineSeparator() {
                  let horizontalPadding: CGFloat = bounds.width * kSNStockCellCellLineSeparatorHorizontalPaddingRatio
                  let lineSeparatorWidth: CGFloat = bounds.width - horizontalPadding * 2;
                  customLineSeparatorView.frame = CGRectMake(horizontalPadding,
                    kSNStockCellCellHeight - kSNStockCellCellLineSeparatorHeight,
                    lineSeparatorWidth,
                    kSNStockCellCellLineSeparatorHeight)
                }
              
                // MARK: Public Class API
              
                class func cellHeight() -> CGFloat {
                  return kSNStockCellCellHeight
                }
              
                // MARK: Public API
              
                func configureWithStock(stock: SNStock) {
                  textLabel!.text = stock.ticker as String
                  textLabel!.textColor = primaryTextColor
                  detailTextLabel!.text = stock.name as String
                  detailTextLabel!.textColor = secondaryTextColor
                  setNeedsLayout()
                } 
              }
              

              要禁用默认行分隔符,tableView.separatorStyle = UITableViewCellSeparatorStyle.None;。消费端比较简单,见下例:

              private func stockCell(tableView: UITableView, indexPath:NSIndexPath) -> UITableViewCell {
                var cell : SNStockCell? = tableView.dequeueReusableCellWithIdentifier(stockCellReuseIdentifier) as? SNStockCell
                if (cell == nil) {
                  cell = SNStockCell(reuseIdentifier:stockCellReuseIdentifier, primaryTextColor:primaryTextColor, secondaryTextColor:secondaryTextColor)
                }
                cell!.configureWithStock(stockAtIndexPath(indexPath))
                cell!.showsCustomLineSeparator = true
                cell!.customLineSeparatorColor = tintColor
                return cell!
              }
              

              【讨论】:

                【解决方案15】:

                我无法隐藏特定单元格上的分隔符,除非使用以下解决方法

                - (void)layoutSubviews {
                    [super layoutSubviews];
                    [self hideCellSeparator];
                }
                // workaround
                - (void)hideCellSeparator {
                    for (UIView *view in  self.subviews) {
                        if (![view isKindOfClass:[UIControl class]]) {
                            [view removeFromSuperview];
                        }
                    }
                }
                

                【讨论】:

                  【解决方案16】:

                  Swift 中使用 iOS 8.4

                  /*
                      Tells the delegate that the table view is about to draw a cell for a particular row. (optional)
                  */
                  override func tableView(tableView: UITableView,
                                          willDisplayCell cell: UITableViewCell,
                                          forRowAtIndexPath indexPath: NSIndexPath)
                  {
                      if indexPath.row == 3 {
                          // Hiding separator line for only one specific UITableViewCell
                          cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
                      }
                  }
                  

                  注意:上面的这个 sn-p 将在 UITableView 上使用动态单元格。您可能会遇到的唯一问题是当您使用具有类别的静态单元格时,分隔符类型不同于无以及表格视图的分组样式。事实上,在这种特殊情况下,它不会隐藏每个类别的最后一个单元格。为了克服这个问题,我发现的解决方案是将单元格分隔符(通过 IB)设置为无,然后手动(通过代码)创建和添加您的线视图到每个单元格。例如,请查看下面的 sn-p:

                  /*
                  Tells the delegate that the table view is about to draw a cell for a particular row. (optional)
                  */
                  override func tableView(tableView: UITableView,
                      willDisplayCell cell: UITableViewCell,
                      forRowAtIndexPath indexPath: NSIndexPath)
                  {
                      // Row 2 at Section 2
                      if indexPath.row == 1 && indexPath.section == 1 {
                          // Hiding separator line for one specific UITableViewCell
                          cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
                  
                          // Here we add a line at the bottom of the cell (e.g. here at the second row of the second section).
                          let additionalSeparatorThickness = CGFloat(1)
                          let additionalSeparator = UIView(frame: CGRectMake(0,
                              cell.frame.size.height - additionalSeparatorThickness,
                              cell.frame.size.width,
                              additionalSeparatorThickness))
                          additionalSeparator.backgroundColor = UIColor.redColor()
                          cell.addSubview(additionalSeparator)
                      }
                  }
                  

                  【讨论】:

                  • 在我的项目中,这适用于静态单元格,但不适用于动态单元格。在后一种情况下,最后一个单元格的内容向右移动(就像分隔线一样)。任何想法,为什么会发生这种情况?
                  • 上述答案的第一个 sn-p 将在 UITableView 上使用动态单元格。您可能会遇到的唯一问题是当您使用具有类别的静态单元格时,分隔符类型不同于无以及表格视图的分组样式。事实上,在这种特殊情况下,它不会隐藏每个类别的最后一个单元格。为了克服这个问题,我发现的解决方案是将单元格分隔符(通过 IB)设置为无,然后手动(通过代码)创建和添加您的线视图到每个单元格。例如,请检查上面答案的第二个 sn-p。
                  • 它会移动文本(标题),所以没用!
                  【解决方案17】:

                  对于 Swift 2:

                  将以下行添加到viewDidLoad()

                  tableView.separatorColor = UIColor.clearColor()
                  

                  【讨论】:

                    【解决方案18】:

                    对于 iOS7 及更高版本,更简洁的方法是使用 INFINITY 而不是硬编码值。您不必担心屏幕旋转时更新单元格。

                    if (indexPath.row == <row number>) {
                        cell.separatorInset = UIEdgeInsetsMake(0, INFINITY, 0, 0);
                    }
                    

                    【讨论】:

                    • 警告:在 iOS9 上使用 INFINITY 会导致运行时异常
                    【解决方案19】:

                    我不相信这种方法适用于动态单元格的任何情况...

                    if (indexPath.row == self.newCarArray.count-1) {
                      cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
                    }
                    

                    对于动态单元格使用哪种 tableview 方法无关紧要,您更改了 inset 属性的单元格现在将始终设置 inset 属性,每次出队时都会导致缺少行分隔符的横冲直撞...直到你自己改变它。

                    这样的事情对我有用:

                    if indexPath.row == franchises.count - 1 {
                      cell.separatorInset = UIEdgeInsetsMake(0, cell.contentView.bounds.width, 0, 0)
                    } else {
                      cell.separatorInset = UIEdgeInsetsMake(0, 0, cell.contentView.bounds.width, 0)
                    }
                    

                    这样你在每次加载时更新你的数据结构状态

                    【讨论】:

                      【解决方案20】:

                      我的开发环境是

                      • Xcode 7.0
                      • 7A220 斯威夫特 2.0
                      • iOS 9.0

                      以上答案对我来说并不完全有效

                      经过尝试,我的最终解决方案是:

                      let indent_large_enought_to_hidden:CGFloat = 10000
                      cell.separatorInset = UIEdgeInsetsMake(0, indent_large_enought_to_hidden, 0, 0) // indent large engough for separator(including cell' content) to hidden separator
                      cell.indentationWidth = indent_large_enought_to_hidden * -1 // adjust the cell's content to show normally
                      cell.indentationLevel = 1 // must add this, otherwise default is 0, now actual indentation = indentationWidth * indentationLevel = 10000 * 1 = -10000
                      

                      效果如下:

                      【讨论】:

                        【解决方案21】:

                        我的要求是隐藏第 4 和第 5 个单元格之间的分隔符。我做到了

                            -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
                            if(indexPath.row == 3)
                            {
                                cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);
                            }
                        }
                        

                        【讨论】:

                          【解决方案22】:
                          in viewDidLoad() {    
                          
                             tableView.separatorStyle = UITableViewCellSeparatorStyle.None 
                          
                          }
                          

                          【讨论】:

                          • 请详细说明这段代码是如何回答这个问题的。
                          • 这会删除整个表格的分隔符,而不是特定单元格
                          【解决方案23】:

                          使用这个子类,设置separatorInset不适用于iOS 9.2.1,内容会被压缩。

                          @interface NSPZeroMarginCell : UITableViewCell
                          
                          @property (nonatomic, assign) BOOL separatorHidden;
                          
                          @end
                          
                          @implementation NSPZeroMarginCell
                          
                          - (void) layoutSubviews {
                              [super layoutSubviews];
                          
                              for (UIView *view in  self.subviews) {
                                  if (![view isKindOfClass:[UIControl class]]) {
                                      if (CGRectGetHeight(view.frame) < 3) {
                                          view.hidden = self.separatorHidden;
                                      }
                                  }
                              }
                          }
                          
                          @end
                          

                          https://gist.github.com/liruqi/9a5add4669e8d9cd3ee9

                          【讨论】:

                            【解决方案24】:

                            willdisplaycell:

                            cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
                            

                            【讨论】:

                              【解决方案25】:

                              正如(许多)其他人指出的那样,您可以轻松隐藏 所有 UITableViewCell 分隔符,只需为整个 UITableView 本身关闭它们即可;例如在你的 UITableViewController 中

                              - (void)viewDidLoad {
                                  ...
                                  self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
                                  ...
                              }
                              

                              不幸的是,这是一个真正的 PITA,以 每个细胞 为基础,这正是您真正要问的。

                              就我个人而言,我再次尝试了许多更改cell.separatorInset.left 的排列方式,正如(许多)其他人所建议的那样,但问题是,引用 Apple 的话(强调添加):

                              "...您可以使用此属性在当前单元格的内容与表格的左右边缘之间添加空格。正插入值移动单元格内容 以及向内和远离表格边缘的单元格分隔符..."

                              因此,如果您尝试通过将分隔符向右推到屏幕外来“隐藏”分隔符,您最终也会缩进单元格的 contentView。正如 crifan 所建议的那样,您可以尝试通过适当地设置 cell.indentationWidthcell.indentationLevel 来补偿这种令人讨厌的副作用以将所有内容移回,但我发现这也不可靠(内容仍然缩进...... )。

                              我发现的最可靠的方法是在一个简单的 UITableViewCell 子类中覆盖 layoutSubviews 并设置 right 插入,使其与左插入,使分隔符的宽度为 0所以不可见[这需要在 layoutSubviews 中完成以自动处理旋转]。我还在我的子类中添加了一个方便的方法来打开它。

                              @interface MyTableViewCellSubclass()
                              @property BOOL separatorIsHidden;
                              @end
                              
                              @implementation MyTableViewCellSubclass
                              
                              - (void)hideSeparator
                              {
                                  _separatorIsHidden = YES;
                              }
                              
                              - (void)layoutSubviews
                              {
                                  [super layoutSubviews];
                              
                                  if (_separatorIsHidden) {
                                      UIEdgeInsets inset = self.separatorInset;
                                      inset.right = self.bounds.size.width - inset.left;
                                      self.separatorInset = inset;
                                  }
                              }
                              
                              @end
                              

                              警告:没有可靠的方法来恢复 原始 右插图,因此您不能“取消隐藏”分隔符,因此我使用不可逆的 hideSeparator 方法( vs 暴露 separatorIsHidden)。请注意,separatorInset 在重复使用的单元格中持续存在,因此,由于您无法“取消隐藏”,因此您需要将这些隐藏的分隔符单元格隔离在它们自己的重用标识符中。

                              【讨论】:

                                【解决方案26】:

                                在 iOS9 上,我遇到的问题是更改分隔符插入也会影响 text- 和 detailLabel 的定位。

                                我用这个解决了

                                override func layoutSubviews() {
                                    super.layoutSubviews()
                                
                                    separatorInset = UIEdgeInsets(top: 0, left: layoutMargins.left, bottom: 0, right: width - layoutMargins.left)
                                }
                                

                                【讨论】:

                                • 不适用于 UITableViewCell 类 - TextLabel 和DetailedTextLabel 远离单元格。
                                【解决方案27】:

                                在您的 UITableViewCell 子类中,覆盖 layoutSubviews 并隐藏 _UITableViewCellSeparatorView。在 iOS 10 下工作。

                                override func layoutSubviews() {
                                    super.layoutSubviews()
                                
                                    subviews.forEach { (view) in
                                        if view.dynamicType.description() == "_UITableViewCellSeparatorView" {
                                            view.hidden = true
                                        }
                                    }
                                }
                                

                                【讨论】:

                                • 上述解决方案均无效,这适用于 ios 12
                                • 这可能会因为访问私有 API 而被 App Store 拒绝。
                                【解决方案28】:
                                cell.separatorInset = UIEdgeInsetsMake(0.0, cell.bounds.size.width, 0.0, -cell.bounds.size.width)
                                

                                在 iOS 10.2 中运行良好

                                【讨论】:

                                  【解决方案29】:

                                  斯威夫特:

                                  public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                                  
                                      ...
                                  
                                      // remove separator for last cell
                                      cell.separatorInset = indexPath.row < numberOfRowsInSection-1
                                          ? tableView.separatorInset
                                          : UIEdgeInsets(top: 0, left: tableView.bounds.size.width, bottom: 0, right: 0)
                                  
                                      return cell
                                  }
                                  

                                  目标-C:

                                  - (UITableViewCell *)tableView:(UITableView *)tableView
                                       cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                                  
                                      ...
                                  
                                      // remove separator for last cell
                                      cell.separatorInset = (indexPath.row < numberOfRowsInSection-1)
                                          ? tableView.separatorInset
                                          : UIEdgeInsetsMake(0.f, tableView.bounds.size.width, 0.f, 0.f);
                                  
                                      return cell;
                                  }
                                  

                                  【讨论】:

                                    【解决方案30】:

                                    Swift 3、Swift 4 和 Swift 5 中,您可以像这样为 UITableViewCell 编写扩展:

                                    extension UITableViewCell {
                                      func separator(hide: Bool) {
                                        separatorInset.left = hide ? bounds.size.width : 0
                                      }
                                    }
                                    

                                    然后您可以按如下方式使用它(当单元格是您的单元格实例时):

                                    cell.separator(hide: false) // Shows separator 
                                    cell.separator(hide: true) // Hides separator
                                    

                                    最好将表格视图单元格的宽度分配为左插图,而不是为其分配一些随机数。因为在某些屏幕尺寸中,可能不是现在,但将来您的分隔符仍然可见,因为该随机数可能不够。此外,在 iPad 的横向模式下,您不能保证您的分隔符始终不可见。

                                    【讨论】:

                                    • 这不适用于分组样式的 UITableView。您有分组案例的解决方案吗?
                                    猜你喜欢
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    相关资源
                                    最近更新 更多