Some tips for setting up the UITableView split line in iOS application development

  • 2020-05-17 06:36:36
  • OfStack

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

In fact, it only needs the following to realize the control of the dividing line.

-(void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath *)indexPath
    // The following lines of code are used to set this cell The position of the upper and lower lines
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
    [cell setLayoutMargins:UIEdgeInsetsZero];
    }     // Add the following in accordance with the author's final meaning 1 Segment, to control the position of the bottom line, so press here stackflow Let's add the above.
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];

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

- (void)tableView:(UITableView )tableView willDisplayHeaderView:(UIView )view forSection:(NSInteger)section
// Background color
view.tintColor = [UIColor blueColor];
// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor redColor]]; // On the other 1 This method sets the background color
// header.contentView.backgroundColor = [UIColor blackColor];

No dividing lines are shown
Modify the UITableView dividing line through tableFooterView:
When using UITableView, if there is no data/little data, it will be found that even cell with no data will have a dividing line, which is not beautiful. Usually, we hope that only cell with data will show the corresponding dividing line, while cell without data will not show the dividing line.
There are two common approaches:

The first option is to first unshow the spline and then customize cell to look like a spline by adding an view of height 1 to the bottom of the cell. This view will only be displayed when cell has data to show, and that will do the trick.

The second way is to do this without either undisplaying the split line or customizing the cell:

self.tableView.tableFooterView = [[UIView alloc] init];

Run the display, and the discovery has served our purpose. It's obviously more convenient.

Related articles: