我在定制一个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;
}

其他回答

iOS 7和8的更好解决方案

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    DLog(@"");
    if (cell && indexPath.row == 0 && indexPath.section == 0) {

        DLog(@"cell.bounds.size.width %f", cell.bounds.size.width);
        cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.0f);
    }
}

如果你的应用程序是可旋转的-使用3000.0f作为左插入常数或在运行中计算它。 如果你尝试设置右插入,你会在iOS 8的单元格左侧看到分隔符。

在Swift 3, Swift 4和Swift 5中,你可以像这样写一个UITableViewCell扩展:

extension UITableViewCell {
  func separator(hide: Bool) {
    separatorInset.left = hide ? bounds.size.width : 0
  }
}

然后你可以这样使用它(当cell是你的cell实例时):

cell.separator(hide: false) // Shows separator 
cell.separator(hide: true) // Hides separator

将表格视图单元格的宽度赋为左插入值比赋给它一些随机数更好。因为在某些屏幕尺寸中,也许不是现在,但将来你的分隔符仍然是可见的因为这个随机数可能不够。此外,在iPad横屏模式下,你不能保证你的分隔符总是不可见。

正如(许多)其他人指出的那样,你可以通过简单地关闭整个UITableView本身来轻松隐藏所有UITableViewCell分隔符;例如在你的UITableViewController中

- (void)viewDidLoad {
    ...
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    ...
}

不幸的是,在每个单元格的基础上,这是一个真正的PITA,这是你真正要求的。

就我个人而言,我已经尝试了许多改变cell.separatorInset的排列。正如(许多)其他人所建议的那样,再次向左,但问题是,引用苹果的话(强调):

"...您可以使用此属性在当前单元格内容与表的左右边缘之间添加空格。正插入值将单元格内容和单元格分隔符向内移动,远离表格边缘……”

因此,如果你试图通过将分隔符推到屏幕的右侧来“隐藏”分隔符,你最终也会缩进你的单元格的contentView。正如crifan所建议的,您可以尝试通过设置cell来补偿这种讨厌的副作用。indentationWidth和cell。indentationLevel适当地将所有内容移回,但我发现这也是不可靠的(内容仍然缩进…)

我发现的最可靠的方法是在一个简单的UITableViewCell子类中覆盖layoutSubviews,并设置右插入,使它命中左插入,使分隔符有0宽度,因此不可见[这需要在layoutSubviews中自动处理旋转]。我还向我的子类添加了一个方便的方法来打开它。

@interface MyTableViewCellSubclass()
@property BOOL separatorIsHidden;
@end

@implementation MyTableViewCellSubclass

- (void)hideSeparator
{
    _separatorIsHidden = YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    if (_separatorIsHidden) {
        UIEdgeInsets inset = self.separatorInset;
        inset.right = self.bounds.size.width - inset.left;
        self.separatorInset = inset;
    }
}

@end

警告:没有一个可靠的方法来恢复原始的右插入,所以你不能“取消隐藏”分隔符,因此我为什么要使用一个不可逆的hideSeparator方法(vs暴露separatorIsHidden)。请注意,separatorInset在重用的单元格之间持续存在,因为你不能“取消隐藏”,你需要将这些隐藏的分隔单元格隔离在它们自己的reuseIdentifier中。

在使用iOS 8.4的Swift中:

/*
    Tells the delegate that the table view is about to draw a cell for a particular row. (optional)
*/
override func tableView(tableView: UITableView,
                        willDisplayCell cell: UITableViewCell,
                        forRowAtIndexPath indexPath: NSIndexPath)
{
    if indexPath.row == 3 {
        // Hiding separator line for only one specific UITableViewCell
        cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
    }
}

Note: this snippet above will work on UITableView using dynamic cells. The only problem that you can encounter is when you use static cells with categories, a separator type different than none and a grouped style for the table view. In fact, in this particular case it will not hide the last cell of each category. For overcoming that, the solution that I found was to set the cell separator (through IB) to none and then creating and adding manually (through code) your line view to each cell. For an example, please check the snippet below:

/*
Tells the delegate that the table view is about to draw a cell for a particular row. (optional)
*/
override func tableView(tableView: UITableView,
    willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath)
{
    // Row 2 at Section 2
    if indexPath.row == 1 && indexPath.section == 1 {
        // Hiding separator line for one specific UITableViewCell
        cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)

        // Here we add a line at the bottom of the cell (e.g. here at the second row of the second section).
        let additionalSeparatorThickness = CGFloat(1)
        let additionalSeparator = UIView(frame: CGRectMake(0,
            cell.frame.size.height - additionalSeparatorThickness,
            cell.frame.size.width,
            additionalSeparatorThickness))
        additionalSeparator.backgroundColor = UIColor.redColor()
        cell.addSubview(additionalSeparator)
    }
}

对于iOS7或更高版本,更简洁的方法是使用INFINITY而不是硬编码的值。当屏幕旋转时,您不必担心更新单元格。

if (indexPath.row == <row number>) {
    cell.separatorInset = UIEdgeInsetsMake(0, INFINITY, 0, 0);
}