Preface
When we create a tableView, have you noticed that there will be blank on the left side of the UITableViewCell? And we have the following requirements in development: a complete dividing line is needed (removing the annoying blank part, that is, the width of the dividing line == the width of the screen).
So I will talk about how to remove the blank parts and display the complete dividing line.
Here I provide two methods:
The first method is also the most commonly used method, and it is also used when we customize the cell. That is, remove the default dividing line of the tableView, customize the cell, and override the setFrame: method
The following is the specific code implementation:
Step 1: Remove the default split line of the system
// Set the style of the dividing line to None. = UITableViewCellSeparatorStyleNone;
tableView has a separatorStyle property, that is, the style of the split line. This is an enum type. When we click on it and enter its attribute, we will find the following code:
typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) { UITableViewCellSeparatorStyleNone, //Do not display the split line UITableViewCellSeparatorStyleSingleLine,// Single line UITableViewCellSeparatorStyleSingleLineEtched // This separation only supports grouping stylesheet views // This separator style is only supported for grouped style table views currently }
Step 2: Rewrite setFrame: Method
Note that override setFrame: The method requires us to write it in the UITableViewCell. As mentioned above, this method is suitable for custom cells.
Here is the code:
- (void)setFrame:(CGRect)frame { += 1; // Increase the y value of the cell by 1 (adjust according to the height of the dividing line you need) -= 1; // Reduce the cell's height by 1 [super setFrame:frame]; // Don't forget to rewrite the parent class method}
Through the above two steps, the system default dividing line will be removed and our own dividing line will be generated. Is this method very simple? If you need to customize the color of the dividing line, you only need to set `separatorColor` to the color you need.
The second method is also very simple. This method does not require us to customize the cell, and it can also be successful using the default tableViewcell. What needs to be explained here is:
In ios7, there will be a default 15 pixel blank on the left side of UITableViewCell.setSeparatorInset:UIEdgeInsetsZero
Can remove the blanks.
ios8,setSeparatorInset:UIEdgeInsetsZero
The settings are no longer working.
Here is the solution. First, add the following code to the viewDidLoad method:
if ([ respondsToSelector:@selector(setSeparatorInset:)]) { // If the tableView responds to setSeparatorInset: this method, we set the inner margin of the tableView split line to 0. [ setSeparatorInset:UIEdgeInsetsZero]; } if ([ respondsToSelector:@selector(setLayoutMargins:)]) { // If the tableView responds to setLayoutMargins: This method, we set the spacing distance of the tableView split line to 0. [ setLayoutMargins:UIEdgeInsetsZero]; }
Then add the following code to the proxy method of UITableView
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { // The meaning of these two sentences is the same as the codes above, so 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 entire content of this article. Through the above two steps, the cell's dividing line can be fully displayed. Friends, try it quickly. If there is any better way, or other ideas, you can leave a message to communicate. At the same time, valuable opinions are welcome. I hope that the content of this article will be of some help to everyone's study or work.