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

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


当前回答

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

其他回答

设置separatorInset。right = . greatestfinitemagnitude。

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

在你的UITableViewCell子类中,覆盖layoutSubviews并隐藏_UITableViewCellSeparatorView。适用于iOS 10。

override func layoutSubviews() {
    super.layoutSubviews()

    subviews.forEach { (view) in
        if view.dynamicType.description() == "_UITableViewCellSeparatorView" {
            view.hidden = true
        }
    }
}

如果你不想自己画分隔符,使用这个:

  // Hide the cell separator by moving it to the far right
  cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);

这个API只能从ios7开始使用。

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