UITableView: the proper way to display a separator for the last cell

后端 未结 14 931
礼貌的吻别
礼貌的吻别 2020-12-29 02:23

The question is what\'s the right-most way to display a separator in the last cell in a table/section.

Basically this is what I am after.

14条回答
  •  隐瞒了意图╮
    2020-12-29 03:07

    I had a similar issue and found a workaround. Apple hides the last uitableviewcell separator and then displays it if you select the cell or if you call select and deselect on that cell.

    So I taught the best is in - (void)layoutSubviews. The separator view is called _separatorView and it's a private property. Using the cell's selected / highlighted states you can come up with something like this:

    - (void)layoutSubviews
    {
        // Call super so the OS can do it's layout first
        [super layoutSubviews];
    
        // Get the separator view
        UIView *separatorView = [self valueForKey:@"_separatorView"];
        // Make the custom inset
        CGRect newFrame = CGRectMake(0.0, 0.0, self.bounds.size.width, separatorView.frame.size.height);
        newFrame = CGRectInset(newFrame, 15.0, 0.0);
        [separatorView setFrame:newFrame];
    
        // Show or hide the bar based on cell state        
        if (!self.selected) {
            separatorView.hidden = NO;
        }
        if (self.isHighlighted) {
            separatorView.hidden = YES;
        }
    }
    

    Something like this.

提交回复
热议问题