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

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


当前回答

除非使用以下解决方法,否则无法在特定单元格上隐藏分隔符

- (void)layoutSubviews {
    [super layoutSubviews];
    [self hideCellSeparator];
}
// workaround
- (void)hideCellSeparator {
    for (UIView *view in  self.subviews) {
        if (![view isKindOfClass:[UIControl class]]) {
            [view removeFromSuperview];
        }
    }
}

其他回答

所有答案的代码将使单元格填充为零,而不是默认值。我在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;
}

在willdisplaycell:

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

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

我能找到的唯一解决办法就是下一个

extension UITableViewCell {
    func separator(hide: Bool) {
        separatorInset.left = hide ? self.bounds.width * 1.5 : 16 // or whatever you want to be the left inset
    }
}

我不知道为什么,但是self.bounds.width没有像预期的那样工作,所以我乘以1.5。

如果你想隐藏一个单元格,你只需要这样做:

cell.separator(hide: true)

对于其余的单元格,只发送参数为false

cell.separator(hide: false)

在ios7中,UITableView分组样式单元格分隔符看起来有点不同。它看起来有点像这样:

我尝试了凯梅纳兰的回答:

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

然而,这似乎对我不起作用。我不知道为什么。所以我决定使用Hiren的答案,但使用UIView而不是UIImageView,并在iOS 7风格中画出界线:

UIColor iOS7LineColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];

//First cell in a section
if (indexPath.row == 0) {

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
    line.backgroundColor = iOS7LineColor;
    [cell addSubview:line];
    [cell bringSubviewToFront:line];

} else if (indexPath.row == [self.tableViewCellSubtitles count] - 1) {

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(21, 0, self.view.frame.size.width, 1)];
    line.backgroundColor = iOS7LineColor;
    [cell addSubview:line];
    [cell bringSubviewToFront:line];

    UIView *lineBottom = [[UIView alloc] initWithFrame:CGRectMake(0, 43, self.view.frame.size.width, 1)];
    lineBottom.backgroundColor = iOS7LineColor;
    [cell addSubview:lineBottom];
    [cell bringSubviewToFront:lineBottom];

} else {

    //Last cell in the table view
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(21, 0, self.view.frame.size.width, 1)];
    line.backgroundColor = iOS7LineColor;
    [cell addSubview:line];
    [cell bringSubviewToFront:line];
}

如果使用这个,请确保在第二个If语句中插入正确的表视图高度。我希望这对某些人有用。