Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TableView section seperator line

I wanted to add separator line into the table view section. Currently the code for the header section view will be:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    // recast your view as a UITableViewHeaderFooterView
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    header.backgroundView.backgroundColor = [UIColor clearColor];
    header.textLabel.textColor = [UIColor blackColor];
    [header.textLabel setFont:[UIFont fontWithName:@"Rubik-Regular" size:15.0]];

}

enter image description here

like image 957
Nur II Avatar asked Jul 17 '17 08:07

Nur II


People also ask

How to add separator in tableView Swift?

Behaviour 1: Add section header to only the third section. What did I try: Add a one pixel height, tableView width UIView, with all required properties set(color, etc) and add it as a sub-view to the section header view's. Add drop shadow with CGPoint(0,1) to the section header views.

What is tableView section?

A table view displays a single column of vertically scrolling content, divided into rows and sections. Each row of a table displays a single piece of information related to your app. Sections let you group related rows together.

What is separator inset?

The separatorInset property of UITableView is not new. It has been around since iOS 7 as a way to set the default inset for the separator between table view cells. It is a UIEdgeInsets but only the left and right values make any difference.

How do I delete a section separator in Swift?

To hide UITableViewCell separator completely, simply set it's colour to UIColor. clearColor(). This will make the cell separator not visible.


3 Answers

Swift 4

 override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView()
    let separatorView = UIView(frame: CGRect(x: tableView.separatorInset.left, y: footerView.frame.height, width: tableView.frame.width - tableView.separatorInset.right - tableView.separatorInset.left, height: 1))
    separatorView.backgroundColor = UIColor.separatorColor
    footerView.addSubview(separatorView)
    return footerView
}

extension UIColor {
   class var separatorColor: UIColor {
     return UIColor(red: 244.0/255.0, green: 244.0/255.0, blue: 244.0/255.0, alpha: 1.0)
   }
}
like image 104
rshankar Avatar answered Oct 11 '22 13:10

rshankar


If you have

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

would be better to make it there:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    // recast your view as a UITableViewHeaderFooterView
    UITableViewHeaderFooterView *header = // make header here
    header.backgroundView.backgroundColor = [UIColor clearColor];
    header.textLabel.textColor = [UIColor blackColor];
    [header.textLabel setFont:[UIFont fontWithName:@"Rubik-Regular" size:15.0]];
    // make a view with height = 1 attached to header bottom
    UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(0, header.frame.size.height, header.frame.size.width, 1)];
    [separator setBackgroundColor:[UIColor yellowColor]];
    [header addSubview:separator];
    return header;
}
like image 21
Eridana Avatar answered Oct 11 '22 15:10

Eridana


I used below code and it worked for me:

  • Swift version : 4.2
  • Xcode version : 10.3
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView()
    let dummyView = UIView() //just a dummy view to return
    let separatorView = UIView(frame: CGRect(x: tableView.separatorInset.left, y: footerView.frame.height, width: tableView.frame.width - tableView.separatorInset.right - tableView.separatorInset.left, height: 0.5))
    separatorView.backgroundColor = UIColor.white
    footerView.addSubview(separatorView)

    if section == 1 {    
        return footerView
    }
    return dummyView
}
like image 30
Imtiyaz Ahmad Avatar answered Oct 11 '22 13:10

Imtiyaz Ahmad