Objective
When we create a tableview, carefully you have not found UITableViewCell left there will be blank. And we have this need in development: a complete split line (remove annoying whitespace, the width of the split line = screen width).
So I'm going to talk about how to get rid of the blank section and show the complete split line.
Here I offer two ways:
The first method, and also our most common method, is what we use when we customize the cell. That is, remove TableView default line, custom cell, rewrite Setframe: Method can
Here is the specific code implementation:
Step One: Remove the default split line from the system
Sets the style of the split line to none.
Self.tableView.separatorStyle = Uitableviewcellseparatorstylenone;
TableView has a Separatorstyle property, which is the style of the split line. This is an enumeration type. When we click on the command to enter his properties, we will find the following code:
typedef ns_enum (Nsinteger, Uitableviewcellseparatorstyle) {
uitableviewcellseparatorstylenone,//Do not show split line
uitableviewcellseparatorstylesingleline,//single line
uitableviewcellseparatorstylesinglelineetched
// This type of detach only supports grouping style sheet view
//This separator style are only supported for grouped style table views currently
}
Step two: Rewrite the Setframe: method
Note Rewrite Setframe: The method needs to be written in UITableViewCell, as mentioned above, which is useful for customizing the cell.
Here's the code:
-(void) Setframe: (CGRect) Frame {
frame.origin.y = 1; Increase the Y value of the cell by 1 (adjusted according to the height of the line you want to split)
frame.size.height = 1;//Let the height of the cell be reduced by 1
[Super setframe:frame];/Don't forget to rewrite the parent class method
}
By using the above two steps, you will remove the default line of the system and generate our own split line. Is this method very simple? If you need to customize the color of the split line, just set ' separatorcolor ' for the color you need.
The second method is also very simple, this method does not require us to customize the cell, using the default Tableviewcell can also be successful. Here's what you need to say:
In iOS7, there will be a default of 15 pixels on the left side of the UITableViewCell. Setting setSeparatorInset:UIEdgeInsetsZero can remove blanks.
The settings in the iOS8 are setSeparatorInset:UIEdgeInsetsZero no longer working.
Here's the workaround, first adding the following code to the Viewdidload method:
if ([Self.tableview respondstoselector: @selector (setseparatorinset:)]) {
//if TableView responds to Setseparatorinset: In this way, we set the inner margin of the TableView split line to 0.
[Self.tableview Setseparatorinset:uiedgeinsetszero];
}
if ([Self.tableview respondstoselector: @selector (setlayoutmargins:)]) {
//if TableView responds to Setlayoutmargins: This method , we set the spacing distance of the TableView split line to 0.
[Self.tableview Setlayoutmargins:uiedgeinsetszero];
}
Then add the following code to the UITableView proxy method
-(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: (NSIndexPath *) Indexpath {
//The meaning of the two sentences is the same as the above two codes, and does not explain the
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 can also achieve the cell's split line to display the complete. Little friends, try it quickly. If there is any better way, or other ideas can be exchanged messages. At the same time, it is very welcome to make valuable comments. I hope the content of this article for everyone's study or work can bring certain help.