Подтвердить что ты не робот

Удалить разделительную линию только для одной ячейки

Я пытаюсь удалить разделитель для одного 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;
}

(It static cells.) Когда я запускаю приложение. Линия разделителя все еще существует. Как удалить строку разделителя для одного cell?

4b9b3361

Ответ 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, 1000, 0, 0);

Ответ 2

Вы можете просто визуально скрыть разделитель с помощью свойства separatorInset:

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

Ответ 3

Swift 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), для которого вы не хотите, чтобы линия разделения отображалась.

Ответ 5

Просто добавьте следующее в ячейку, которую вы не хотите использовать разделитель (swift 3):

override func awakeFromNib() {
    super.awakeFromNib()

    // remove separator
    self.separatorInset = UIEdgeInsetsMake(0, 1000, 0, 0)
}

Ответ 6

Для тех, кто борется с этим для 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)
    }
}

Ответ 7

cell.separatorInset = UIEdgeInsetsMake(0.0 , cell.bounds.size.width , 0.0, -cell.bounds.size.width)

Ответ 8

Другим способом, который немного взломан, является создание пользовательской ячейки представления таблицы с помощью uiView, который действует как вставка разделителя. Затем скройте и покажите это, когда захотите.

Я создал SampleTableViewCell и файл nib с меткой и разделителемLineView

@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

Ответ 9

Я создал расширение для UITableViewCell, установка значения 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
            }
        }
    }
}

Ответ 10

Swift 5.1 -

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

Ответ 11

Если вы хотите показать разделитель только видимой ячейке, попробуйте следующее:

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

Ответ 12

Swift 3 удалить разделитель всей ячейки:

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