【问题标题】:Remove separator line for only one cell仅删除一个单元格的分隔线
【发布时间】:2015-07-01 19:53:39
【问题描述】:

我正在尝试删除一个UITableViewCell 的分隔符。我做了以下事情:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    if (indexPath.row == 1)
        cell.separatorInset = UIEdgeInsetsZero;

    return cell;
}

(它是static cells。)当我运行应用程序时。分隔线仍然存在。如何删除一个 cell 的分隔线?

【问题讨论】:

  • 设置 separatorInset 为 UIEdgeInsetsZero 是从左到右显示分隔符。是否仅在行索引 1 处隐藏 separatorInset?
  • 是的。我正在尝试隐藏行索引 1 处的分隔符
  • 仅删除单元格上的 1 个分隔符很棘手,因为您要么得到所有分隔符插入,要么没有。我通常最终实现了我自己的分隔视图,它只是一个 1 像素高度的 uiview。然后,设计显示和隐藏分隔符视图。可能有一些更好的想法。我热衷于向我们学习。
  • 你能举个例子吗?另外在情节提要中,我应该将分隔符设置为无吗?
  • 是的。如果你想有一个自定义的分隔符视图,你需要将分隔符设置为无。例如,编写它需要一段时间。如果你能等一下,我会试着把事情大致放在一起。

标签: ios objective-c uitableview


【解决方案1】:

在 iOS 8 上你需要使用:

cell.layoutMargins = UIEdgeInsetsZero

如果您还想与 iOS 7 兼容,您应该执行以下操作:

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

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

添加:如果以前不起作用 - 使用这个。 (来自下面的答案)

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

如果以上都不起作用,你可以这样做:

self.tableView.separatorColor = [UIColor clearColor];

但这会留下 1 个像素的空白空间,并不会真正删除一条线,而是让它变得透明。

【讨论】:

  • 第二种方法是否也支持ios 8?
  • 第一个适用于 iOS 7 及更低版本,第二个适用于 iOS 8。您需要将两者都放入您的应用中,这样它才能针对给定的 iOS 版本运行正确的代码
  • (在您写的第一个答案中,第一个是针对 io8 的。)我需要一个 if 语句来支持这两个版本吗?
  • respondsToSelector 负责检查方法是否存在,即兼容性检查
  • 这些解决方案在我的情况下都没有任何效果
【解决方案2】:

您可以通过separatorInset 属性直观地隐藏分隔符:

tableViewCell.separatorInset = UIEdgeInsets(top: 0, left: 10000, bottom: 0, right: 0)

【讨论】:

    【解决方案3】:

    斯威夫特 4

    iOS 11

    为您需要自定义的特定单元格分配下一个值。

    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, .greatestFiniteMagnitude)
    cell.directionalLayoutMargins = .zero
    

    【讨论】:

      【解决方案4】:

      如果只想隐藏特定类型单元格的分隔线,可以使用以下代码:

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

      将此代码写入您不希望出现分隔线的单元格(它必须是UITableViewCell 的子类)中。

      【讨论】:

      • 我尝试了很多其他的解决方案,但最终结果是最不麻烦的。在 Swift 3.0 中,这是正确的语法:subviews.forEach { view in if type(of: view).description() == "_UITableViewCellSeparatorView" { view.isHidden = true } }
      • Hackiest 的解决方案。
      • 警告 - 这会导致 iOS 10 出现问题。iOS 似乎会在单元格实例之间克隆分隔符属性,并且不同表格视图上完全不同的单元格类开始出现而没有分隔符。
      • 这非常hacky,可能会在未来的iOS版本中中断。
      • 这是使用私有API,即使有效,也存在申请被拒绝的风险
      【解决方案5】:

      只需将以下内容添加到您不想要分隔符的单元格中(swift 3):

      override func awakeFromNib() {
          super.awakeFromNib()
      
          // remove separator
          self.separatorInset = UIEdgeInsetsMake(0, 1000, 0, 0)
      }
      

      【讨论】:

      • 这似乎是 hack..但不推荐用于理想的编程。
      【解决方案6】:

      Swift 5 享受

      //MARK:- In which row you want to hide 
      cell.separatorInset = UIEdgeInsets(top: 0, left: CGFloat.greatestFiniteMagnitude, bottom: 0, right: 0);
      

      【讨论】:

        【解决方案7】:

        对于那些在 iOS 9 上苦苦挣扎的人来说,解决方案与已经给出的答案略有不同。以下是我在 Swift 中解决它的方法:

        override func viewWillAppear(animated: Bool) {
            table.cellLayoutMarginsFollowReadableWidth = false
        }
        
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            if IS_THE_CELL_WITH_NO_SEPARATOR{
                cell.separatorInset = UIEdgeInsetsZero
                cell.preservesSuperviewLayoutMargins = false
                cell.layoutMargins = UIEdgeInsetsZero
            }
        }
        
        func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
            if IS_THE_CELL_WITH_NO_SEPARATOR{
                let size = self.view.bounds.size
                let rightInset = size.width > size.height ? size.width : size.height
                cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, rightInset)
            }
        }
        

        【讨论】:

          【解决方案8】:
          cell.separatorInset = UIEdgeInsetsMake(0.0 , cell.bounds.size.width , 0.0, -cell.bounds.size.width)
          

          【讨论】:

          • 虽然这可能会回答这个问题,但一些上下文和/或解释会很好。也适合未来的读者。
          【解决方案9】:

          另一种有点 hacky 的方法是使用 uiView 创建自定义表格视图单元格,其作用类似于分隔符插入。然后,在需要时隐藏和显示。

          我创建了 SampleTableViewCell 和一个带有 label 和 separatorLineView 的 nib 文件

          @interface SampleTableViewCell : UITableViewCell
          @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
          @property (weak, nonatomic) IBOutlet UIView *separatorLineView;
          @end
          

          然后,在 ViewController 类中

          @interface ViewController () 
          @property (nonatomic) NSArray *items;
          @end
          
          @implementation ViewController
          
          - (void)viewDidLoad {
              [super viewDidLoad];
              self.items = @[@"A", @"B", @"C"];
              [self.tableView registerNib:[UINib nibWithNibName:@"SampleTableViewCell" bundle:nil] forCellReuseIdentifier:@"SampleCell"];
          }
          
          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
              SampleTableViewCell *cell = (SampleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SampleCell" forIndexPath:indexPath];
              cell.titleLabel.text = self.items[indexPath.row];
              if (indexPath.row == 1) {
                  cell.separatorLineView.hidden = YES;
              } else {
                  cell.separatorLineView.hidden = NO;
              }
              return cell;
          }
          
          @end
          

          【讨论】:

            【解决方案10】:

            我为 UITableViewCell 创建了一个 Extension,设置 separatorInset 值会给我带来锚错误,我正在使用 Eureka 表单 Pod。

            extension UITableViewCell {
                func hideSeparator(hide: Bool) {
                    let subviews = self.subviews
                    for view in subviews {
                        if view.classForCoder.description() == "_UITableViewCellSeparatorView" {
                            view.isHidden = hide
                        }
                    }
                }
            }
            
            

            【讨论】:

              【解决方案11】:

              斯威夫特 5.1 -

              override public func layoutSubviews() {
                  super.layoutSubviews()
                  subviews.forEach { (view) in
                      if type(of: view).description() == "_UITableViewCellSeparatorView" {
                          view.isHidden = true
                      }
                  }
              }
              

              【讨论】:

              • 不是一个好主意。访问私有 API 可能会导致您的应用被拒批。此名称将来也可能在您不知情的情况下更改。
              【解决方案12】:

              如果您只想在可见单元格中显示分隔符,您可以试试这个:

              tableView.separatorInset = UIEdgeInsets(top: 0, left: 1000, bottom: 0, right: 0)
              
              let visibleCell = tableView.visibleCells
              
                  for cellV in visibleCell{
                      cellV.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                  }
              

              【讨论】:

                【解决方案13】:

                使UITableView 样式为.plain

                使用下面的代码行

                cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
                

                func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 方法

                【讨论】:

                  【解决方案14】:

                  Swift 3 删除所有单元格的分隔符:

                   override func viewDidLoad() {
                          self.yourTableView.separatorStyle = UITableViewCellSeparatorStyle.none
                      }
                  

                  【讨论】:

                  • 请使用edit链接来解释这段代码是如何工作的,不要只给出代码,因为解释更有可能帮助未来的读者。另见How to Answersource
                  • 更重要的是,这个答案并没有回答所提出的问题。这段代码只是禁用了整个表格的分隔符;一次不选择性地处理一个单元格。
                  猜你喜欢
                  • 2018-03-10
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-05-31
                  • 2021-06-02
                  • 1970-01-01
                  • 2015-01-18
                  • 1970-01-01
                  相关资源
                  最近更新 更多