iOS developed TableView to achieve a complete detailed explanation of the dividing line

  • 2021-08-28 21:20:02
  • OfStack

Preface

When we create an tableView, be careful if you find a blank space on the left side of UITableViewCell. However, we have such a requirement in the development: we need a complete dividing line (remove the annoying blank part, that is, the width of the dividing line = = the width of the screen).

Then I will talk about how to remove the blank part and show the complete dividing line.

Here I offer two methods:

The first method, which is also our most commonly used method, is also used when we customize cell. That is, remove the default dividing line of tableView, customize cell, and rewrite setFrame: Method

The following is the specific code implementation:

Step 1: Remove the system default dividing line


//  Set the style of the dividing line to None.
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

tableView has an separatorStyle attribute, which is the style of the dividing line. This is an enumerated type. If we click on command to enter its attribute, we will find the following code:


typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
 UITableViewCellSeparatorStyleNone, // Do not display dividing lines  
 UITableViewCellSeparatorStyleSingleLine,//  Single line  
 UITableViewCellSeparatorStyleSingleLineEtched
 //  This separation only supports grouped style sheet views 
 // This separator style is only supported for grouped style table views currently
}

Step 2: Override setFrame: Method

Note that overriding setFrame: The method needs to be written in UITableViewCell, and as mentioned above, this method is suitable for customizing cell.

Here's the code:


- (void)setFrame:(CGRect)frame {
 frame.origin.y += 1;  //  Jean cell Adj. y Value increase 1( Adjust according to the height of the dividing line you need )
 frame.size.height -= 1; //  Jean cell The height of minus 1
 [super setFrame:frame]; //  Don't forget to override the parent class method 
}

Through the above two steps, we will remove the default dividing line and generate our own dividing line. Is this method very simple? If you need to customize the color of the divider, just set ` separatorColor ` to the color you need.

The second method is also very simple. This method does not require us to customize cell, and it can be successful by using the default tableViewcell. What needs to be explained here is:

In ios7, there is a default 15-pixel white space on the left side of UITableViewCell setSeparatorInset:UIEdgeInsetsZero Blanks can be removed.

In ios8, setSeparatorInset:UIEdgeInsetsZero The setting of is no longer working.

The following is the solution. First, add the following code to the viewDidLoad method:


if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
//  If tableView Responded setSeparatorInset:  This method , We will tableView The inner margin of the dividing line is set to 0.
  [self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
//  If tableView Responded setLayoutMargins:  This method , We will tableView The spacing of the dividing lines is set to 0.
  [self.tableView setLayoutMargins:UIEdgeInsetsZero];
}

Then add the following code to the proxy method of UITableView


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//  These two sentences have the same meaning as the above two sentences , I won't explain it 
 if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
   [cell setSeparatorInset:UIEdgeInsetsZero];
  }
 if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
   [cell setLayoutMargins:UIEdgeInsetsZero];
  }
}

Summarize

The above is the whole content of this article. Through the above two steps, the dividing line of cell can be displayed completely. Little friends, try it quickly. If there is any better way, or other ideas, you can leave a message and communicate. At the same time, you are very welcome to put forward valuable opinions. I hope the content of this paper can bring 1 certain help to your study or work.


Related articles: