仅删除一个单元格的分隔线
我正在尝试删除一个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
的分隔线?
在iOS 8上,您需要使用:
cell.layoutMargins = UIEdgeInsetsZero
如果您想要与iOS 7兼容,您应该执行以下操作:
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; }
ADD:如果之前没有用 – 请使用此function。 (来自下面的答案)
cell.separatorInset = UIEdgeInsetsMake(0, 1000, 0, 0);
您可以通过separatorInset
属性直观地隐藏分隔separatorInset
:
tableViewCell.separatorInset = UIEdgeInsets(top: 0, left: 10000, bottom: 0, right: 0)
如果要仅为特定类型的单元格隐藏分隔线,可以使用以下代码:
override func layoutSubviews() { super.layoutSubviews() subviews.forEach { (view) in if view.dynamicType.description() == "_UITableViewCellSeparatorView" { view.hidden = true } } }
将此代码写入单元格(它必须是UITableViewCell
的子类),您不希望出现分隔线。
只需将以下内容添加到您不想要分隔符的单元格(swift 3):
override func awakeFromNib() { super.awakeFromNib() // remove separator self.separatorInset = UIEdgeInsetsMake(0, 1000, 0, 0) }
对于那些为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) } }
cell.separatorInset = UIEdgeInsetsMake(0.0 , cell.bounds.size.width , 0.0, -cell.bounds.size.width)
另一种有点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
斯威夫特4
iOS 11
为自定义所需的特定单元格分配下一个值。
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGFloat.greatestFiniteMagnitude) cell.directionalLayoutMargins = .zero
Swift 3删除分隔符所有单元格:
override func viewDidLoad() { self.yourTableView.separatorStyle = UITableViewCellSeparatorStyle.none }