How to Set the Full Width of Separator in Uitableview

How to set the full width of separator in UITableView

I got the answer from this post: iOS 8 UITableView separator inset 0 not working

Just add this code on your UITableViewController

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}

-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}

if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}

Can you change separator width for different tableview cells?

You can change the dimensions of the separator using cell.separatorInset.

For instance this draws a shorter separator for the first three cells:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row < 3 {
cell.separatorInset = UIEdgeInsetsMake(0, 30, 0, 11) // shorter separator
else {
cell.separatorInset = UIEdgeInsetsMake(0, 3, 0, 11) // longer separator
}
}

From the willDisplayCell docs:

A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.

uitableviewcell separator line not displaying in full width

Hey All I got the solution.

I override layoutSubviews in my Custom UITableViewCell and the separators are displayed properly.

- (void)layoutSubviews {
[super layoutSubviews];

for (UIView *subview in self.contentView.superview.subviews) {
if ([NSStringFromClass(subview.class) hasSuffix:@"SeparatorView"]) {
subview.hidden = NO;
}
} }

Separator lines for UITableViewCellStyleSubtitle cells not taking the full width

Add Table View Separator Insets to Zero. Not Just for Table View Cell.

Sample Image

Add this code in cellForRowAtIndexPath:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//....
if ([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0)
{
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
}
}

Integration with your code :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];

if ([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0)
{
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
}

NSDictionary* item = [self.items objectAtIndex:indexPath.row];
cell.textLabel.text = item[@"title"];
cell.detailTextLabel.text = item[@"subtitle"];
UIImage* icon = [UIImage imageNamed:item[@"icon"]];
[cell.imageView setImage:icon];

return cell;
}

See the preview :

Sample Image

specify the separator line width of the table view

to remove all margin settings from the cell. try adding all these to the cell in

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

// Remove insets in UITableViewCell separator

// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
cell.separatorInset = UIEdgeInsetsZero;
}

// Prevent the cell from inheriting the Table View's margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
cell.preservesSuperviewLayoutMargins = NO;
}

// Explictly set cell's layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
cell.layoutMargins = UIEdgeInsetsZero;
}

Separator line not showing full width in iPad

you can directly changed in your Attribute Inspector change separator Inset --> custom and set left --> 0

TableViewAttributesInspector

and in your cell class also change the layout margin

TableViewCellSizeInspector

Update

if want to remove the separator inset from all cells, you need to do two things. First, add these two lines of code to your table view controller's viewDidLoad() method:

override func viewDidLoad() {
super.viewDidLoad()

tableView.layoutMargins = .zero
tableView.separatorInset = .zero
}

Now look for you cellForRowAt method and add this:

cell.layoutMargins = .zero 

Is it possible to increase the UITableView separator line thickness?

The only way of doing it, is setting the separtorStype to UITableViewCellSeparatorStyleNone and then you have two options:

  • Create a custom UITableViewCell with the separator inside it or
  • Create an alternate UITableViewCell with the separator you want and place inside every other cells. If you want to display 3 rows on your table, you should display 5 instead with the alternate cell in rows 2 and 4.

cell separator line in uitableview not covering the complete width

With this codes it will be ok :

In method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath:

cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;

EDIT :
Changed code lines according to this post : iOS 8 UITableView separator inset 0 not working

iOS7 last cell in UITableView section forces full width separator

After some experimentation I've found the only real solution is to set the UITableViewStyle to UITableViewStylePlainand set empty footers using:

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;
}

This will not be a satisfactory solution for some, because UITableViewStylePlain does not provide all of the features of UITableViewStyleGrouped, but it gives me the section without the full-width separator.

Sample Image



Related Topics



Leave a reply



Submit