SoFunction
Updated on 2025-04-13

Some tips for setting tableView cell splitting line in iOS

Preface

For the iOS tableView cell segmentation lines, we rarely use them, which is not the system default, but some projects still require us to change the color or appearance of the segmentation lines to match the tone of the entire project. This Apple has long been thought of for us.

1. Regarding the position of the dividing line.

The position of the split line refers to the split line relative to the tableViewCell. If we want to adjust its position according to requirements, after iOS7.0, a method is provided as follows:

if ([ respondsToSelector:@selector(setSeparatorInset:)]) {
  
  [ setSeparatorInset:UIEdgeInsetsMake(0, 45, 0, 0)];
  
 }

The four parameters of UIEdgeInsets are the distances above, left, bottom, and right of the cell, and are all CGFloat types.

2. Color and style of the dividing line:

a. The color of the cell's split line is not the cell's attribute, it belongs to the separatorColor attribute of the tableView. In this way, we only need to set the attribute value to get the split line of all the colors we want.

[ setSeparatorColor:[UIColor clearColor]];

b. cell style: It is the separatorStyle property of tableView. The system provides us with three styles defined in the enumeration UITableViewCellSeparatorStyle, namely

typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
 UITableViewCellSeparatorStyleNone,
 UITableViewCellSeparatorStyleSingleLine,
 UITableViewCellSeparatorStyleSingleLineEtched // This separator style is only supported for grouped style table views currently
};

The default is UITableViewCellSeparatorStyleSingleLine.

3. TableViewCell dividing line customization

First, you need to remove the dividing line that comes with the cell. Use the following two options: one is to set the color to clearColor, and the other is to set the style to UITableViewCellSeparatorStyleNone.

Two general methods to use to customize cell segmentation lines

a. Put the customized split line as a View on the cell's contentView. Be sure to pay attention to the reuse issue, so this view should be added when the cell is initialized. The sample code is as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 UITableViewCell *cell = nil;
 cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
 if (cell == nil) {
  cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
   = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"huicellacce"]];
   = [UIColor clearColor];
//   = YES;
  UIImageView *imageViewSepE = [[UIImageView alloc]initWithFrame:CGRectMake(47, 49, 200, 1)];
   = [UIImage imageNamed:@"godline"];
  [ addSubview:imageViewSepE];

 }
}

b. It is quite complicated and uses the underlying framework.

- (void)drawRect:(CGRect)rect { 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context, rect); 

CGContextSetStrokeColorWithColor(context, [UIColorcolorWithHexString:@"ffffff"].CGColor);
 CGContextStrokeRect(context, CGRectMake(5, -1,  - 10, 1)); //Lower split lineCGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"e2e2e2"].CGColor); 
CGContextStrokeRect(context, CGRectMake(5, ,  - 10, 1));
 }

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.