代码之家  ›  专栏  ›  技术社区  ›  CodingWithoutComments

是否有办法从UITableView中的单个单元格中删除分隔线?

  •  12
  • CodingWithoutComments  · 技术社区  · 15 年前

    7 回复  |  直到 15 年前
        1
  •  17
  •   Vinodh    11 年前

    你最好的办法可能是把桌子摆好 separatorStyle UITableViewCellSeparatorStyleNone 手动添加/绘制一条线(可能在 tableView:cellForRowAtIndexPath: )当你想要的时候。

        2
  •  9
  •   hanleyp    14 年前

    在tableView中:cellForRowAtIndexPath:

    ...
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    
        // Drawing our own separatorLine here because I need to turn it off for the
        // last row. I can only do that on the tableView and on on specific cells.
        // The y position below has to be 1 less than the cell height to keep it from
        // disappearing when the tableView is scrolled.
        UIImageView *separatorLine = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, cell.frame.size.height - 1.0f, cell.frame.size.width, 1.0f)];
        separatorLine.image = [[UIImage imageNamed:@"grayDot"] stretchableImageWithLeftCapWidth:1 topCapHeight:0];
        separatorLine.tag = 4;
    
        [cell.contentView addSubview:separatorLine];
    
        [separatorLine release];
    }
    
    // Setup default cell setttings.
    ...
    UIImageView *separatorLine = (UIImageView *)[cell viewWithTag:4];
    separatorLine.hidden = NO;
    ...
    // In the cell I want to hide the line, I just hide it.
    seperatorLine.hidden = YES;
    ...
    

    在viewDidLoad中:

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
    
        3
  •  6
  •   marshy101    10 年前

    这对我很管用。

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

    它将线条左右两侧的插图推到死点,死点为160,这使得线条不可见。

        4
  •  5
  •   Ash R    10 年前
    self.separatorInset = UIEdgeInsetsMake(0, CGRectGetWidth(self.frame)/2, 0, CGRectGetWidth(self.frame)/2);
    
        5
  •  2
  •   Trianna Brannon    9 年前

    我能够隐藏多个不同单元格的分隔线,方法是将左插图设置为整个单元格边界宽度的大小。将“indexPath.row==0”替换为删除分隔行所需的行号。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    
    if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 8 || indexPath.row == 9) {
        cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);
    }
    else {
        cell.separatorInset = UIEdgeInsetsMake(0, 24, 0, 0);
    }
    
    
    return cell;
    }
    
        6
  •  1
  •   Kostas Tsoleridis    8 年前

    对于使用swift且只想隐藏特定类型细胞的分隔线的有相同问题的人,可采用以下方法:

     override func layoutSubviews() {
        super.layoutSubviews()
        subviews.forEach { (view) in
            if view.dynamicType.description() == "_UITableViewCellSeparatorView" {
                view.hidden = true
            }
        }
    }
    
        7
  •  0
  •   Zorayr    10 年前

    实现这一点的最佳方法是关闭默认的行分隔符,即子类 UITableViewCell contentView -请参见下面用于表示类型为的对象的自定义单元格 SNStock 有两个字符串属性, ticker name :

    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!
    }