Change the UITableView separatorColor for a single UITableViewCell

前端 未结 3 1249
醉梦人生
醉梦人生 2021-01-02 03:17

I have a UITableView that uses a variety of custom UITableViewCells.

I\'d like to be able to have one of these table cells appear with a di

相关标签:
3条回答
  • 2021-01-02 03:58

    Disclaimer - this worked for me at the time under my specific circumstances. It is not guaranteed to work, it appears to no longer work, and I now advise you subclass UITableViewCell.

    Came across this post when looking to set the UITableView.separatorColor differently across groups/sections in a grouped UITableView.

    You don't necessarily need to subclass UITableViewCell. You can try setting tableView.separatorColor on each call to tableView:cellForRowAtIndexPath:.

    For example, if you want the separator to be visible with the default color in the first section, visible in the first row/cell of the second section, and invisible in the rest of the rows in the second section, you can do the following:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            tableView.separatorColor = nil;
            break;
        case 1:
            switch (indexPath.row) {
                case 0:
                    tableView.separatorColor = nil;
                    break;
                case 1:
                    tableView.separatorColor = [UIColor clearColor];
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }
    
    0 讨论(0)
  • 2021-01-02 03:59

    Starting from iOS 7 something like this seems to be working fine:

    1. Set UITableViewCellSeparatorStyleSingleLine on your UITableView
    2. For the cell that you want to change the separator color first hide the separator by changing the insets cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 10000);
    3. Change the background color of the cell that should have different separator color cell.backgroundColor = [UIColor greenColor];

    Now separator will have the same color as your cell's backgroundcolor. Note that all this can be set on your cell's xib file without any code. Also note that you probably want to change cell's contentView's background color to something different than default (probably white) so green color will appear only on the separator line.

    0 讨论(0)
  • 2021-01-02 04:06

    The tableView.separatorColor is global across all cells.

    If you want to further customize these colors, your best bet would be to set separatorStyle property to UITableViewCellSeparatorStyleNone, and override UITableViewCell.

    Then you can draw your own custom seperator in the contentView of the Cell and customize it.

    0 讨论(0)
提交回复
热议问题