我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?

我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。


当前回答

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

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

其他回答

在UITableViewDataSource的cellForRowAtIndexPath方法中

迅速:

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

or :

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

对于默认保证金:

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

显示端到端的分隔符

cell.separatorInset = .zero

objective - c:

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

我的要求是将分隔符隐藏在第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);
    }
}

在willdisplaycell:

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

实现这一点的最好方法是关闭默认的行分隔符,子类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!
}

所有答案的代码将使单元格填充为零,而不是默认值。我在iOS 11 iPad Pro 12中看到了这个问题。”

但我有一个解决方案(“肮脏的hack”),那就是使一个空的部分,将使分隔线隐藏。

下面是我使用的代码:

typedef enum PXSettingsTableSections {
    PXSettingSectionInvite = 0,
    PXSettingSectionAccount,
    PXSettingSectionPrivacy,
    PXSettingSectionCreation,
    PXSettingSectionTest,
    PXSettingSectionAboutHide,  // invisble section just to hide separator Line
    PXSettingSectionFooterHide, // invisble section just to hide separator Line
    PXSettingSectionNumItems,
} PXSettingsTableSectionsType;


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return PXSettingSectionNumItems;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    switch (section) {
        case PXSettingSectionInvite: return 1;
        case PXSettingSectionAccount:return (isSocialLogin) ? 1 : 2;
        case PXSettingSectionPrivacy: return 1;
        case PXSettingSectionCreation: return 2;
        case PXSettingSectionTest: return 3;
        case PXSettingSectionAboutHide: return 3;
        default: return 0;
    }
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    switch(section)
    {
        case PXSettingSectionInvite: return nil;
        case PXSettingSectionAccount: return @"Account";
        case PXSettingSectionPrivacy: return @"Privacy";
        case PXSettingSectionCreation: return @"Someting";
        case PXSettingSectionTest: return @"Test";
        case PXSettingSectionAboutHide: return @" ";
        case PXSettingSectionFooterHide: return @" ";
    }
    return nil;
}


- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {

    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    if (section == PXSettingSectionFooterHide || section == PXSettingSectionAboutHide) {
        // [UIColor clearColor] will not work 
        [header.contentView setBackgroundColor:[UIColor whiteColor]];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    // You can control here the size of one specific section
    if(section == PXSettingSectionAboutHide){
        return 0.0000001; //make it real small
    }
    return 45.0;
}


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

    switch(indexPath.section)
    {
        case PXSettingSectionInvite:
            return self.inviteCell;
        case PXSettingSectionAccount:
            if (isSocialLogin) {
                return self.myWalletCell;
            }
            switch(indexPath.row)
            {
                case 0: return self.changePasswordCell;
                case 1: return self.myWalletCell;
            }
        case PXSettingSectionPrivacy:
            switch(indexPath.row)
        {
            case 0: return self.privateAccountCell;
        }
        case PXSettingSectionCreation:
            switch(indexPath.row)
        {
            case 0: return self.videoResolutionCell;
            case 1: return self.selfieMirrorCell;
        }
        case PXSettingSectionTest:
            switch(indexPath.row)
        {
            case 0: return self.termsOfUseImageCell;
            case 1: return self.attributionCell;
            case 2: return self.aboutCell;
        }
        case PXSettingSectionAboutHide:{
            switch(indexPath.row)
            {
                case 0: return self.clearCacheCell;
                case 1: return self.feedbackCell;
                case 2: return self.logoutCell;
            }
        }
    }

    return self.emptyCell;
}