SoFunction
Updated on 2025-03-04

Some tips for setting the segmentation line of UITableView in iOS application development

For ios7, ios8 and above, it is already quite inconvenient to adjust the split line position of the UITableView cell because margin layout is used internally.

In fact, you only need to do the following to achieve the control of the dividing line.

Copy the codeThe code is as follows:

-(void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//The following lines of code are used to set the position of the up and down line of the cell
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
    [cell setLayoutMargins:UIEdgeInsetsZero];
    }

//According to the author's final meaning, the following paragraph must be added to the bottom line control position, so add it here according to the method on stackflow.
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}

If you want to use the sectionTitle of TableView directly, but want to set its font, color, etc., you can use the following method.

Copy the codeThe code is as follows:

- (void)tableView:(UITableView )tableView willDisplayHeaderView:(UIView )view forSection:(NSInteger)section
{
// Background color
= [UIColor blueColor];
// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[ setTextColor:[UIColor redColor]];

// Another way to set the background color
// = [UIColor blackColor];
}

No dividing line is displayed
Modify the UITableView split line through tableFooterView:
When using UITableView, if there is no data/data, you will find that even cells without data will have dividing lines, which does not look beautiful. Usually, we hope that only cells that display data will display the corresponding dividing lines, while cells that do not display data will not display dividing lines.
There are two common practices:

The first method is to first cancel the display of the split line, then customize the cell, and add a view with a height of 1 at the bottom of the cell, so that it looks like a split line. This view will only be displayed when the cell has data displayed, so that the purpose is achieved.

The second method does not require undisplaying the split line or customizing the cell, but does this directly:

Copy the codeThe code is as follows:

= [[UIView alloc] init];

Run the results and discovery has achieved our goal. It is obvious that this approach is more convenient.