Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Tableview always showing separator lines

I seem to have a weird problem with one of my tableviews where I can't hide the separator lines ( I think they're separator lines, except that they're centred on the screen instead of hard up against the right hand side ).

I create everything programmatically so no storyboard issues here.

You can see the tiny 1px line above each of the cells below.

Screenshot of issue

I load my table using:

    self.tableView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
    self.tableView.dataSource = self
    self.tableView.delegate = self
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    self.tableView.backgroundColor = UIColor.whiteColor()
    self.tableView.scrollEnabled = true
    self.tableView.bounces = false
    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    self.view.addSubview(self.tableView)

I also have a custom header which I implement using the following code:

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    let header:UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView

    header.textLabel.textColor = UIColor.whiteColor()
    header.textLabel.frame = header.bounds
    header.textLabel.textAlignment = NSTextAlignment.Center

    view.tintColor = constants.getTintColor()
    header.textLabel.textColor = UIColor.whiteColor()

}

I've tried loading the table without the willDisplayHeaderView and the issue persists.

I have also tried adding

tableview.separatorStyle = UITableViewCellSeparatorStyle.None 

and

tableView.separatorColor = UIColor.clearColor()

to the following methods:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    tableView.separatorStyle = UITableViewCellSeparatorStyle.None

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    tableView.separatorStyle = UITableViewCellSeparatorStyle.None

    return self.boards.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    tableView.separatorStyle = UITableViewCellSeparatorStyle.None

    return self.boards[section].items.count
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
    tableView.separatorStyle = UITableViewCellSeparatorStyle.None

    return self.boards[section].name
}

EDIT:

The cells are standard UITableViewCells with alternating colors for the cells, this is being set through:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

    cell.selectionStyle = UITableViewCellSelectionStyle.None

    if indexPath.row % 2 == 0 {
        // Even
        cell.backgroundColor = constants.UIColorFromRGB(0x1EACE0)
    } else {
        // Odd
        cell.backgroundColor = constants.UIColorFromRGB(0x6FBEE5)
    }

    cell.textLabel?.textColor = UIColor.whiteColor()
    cell.textLabel?.text = self.boards[indexPath.section].items[indexPath.row].title

    return cell
}

EDIT 2:

I've now added separator lines in and these aren't separators because you can still see them below the separator line.

enter image description here

HELP! I'm confused. I have a number of other tableviews setup the same (as far as I can see) and they work fine.

Thanks SO!

like image 995
bwash70 Avatar asked May 11 '15 04:05

bwash70


People also ask

How do I remove a separator from a table view?

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

Can we use TableView in SwiftUI?

In this blog post, I'll introduce the collection view Table in SwiftUI and explain how to leverage this view on iOS 16 to build a multiplatform app. SwiftUI provides several collection views that you can use to assemble other views into dynamic groupings with complex, built-in behaviors.


2 Answers

You can either use the Table View property for the seperatorStyle and use the property as such UITableViewCellSeparatorStyleNone

or use remove the separator from the StoryBoard

enter image description here

Set the property of the Seperator to None

Or one thing more while you are using header and Footer so the separator line would be Zero but there might be a extra separator or header and footer so refer to this link Eliminate extra separators below UITableView

like image 182
Fatti Khan Avatar answered Nov 15 '22 20:11

Fatti Khan


in your viewDidLoad() function, add this:

tableView.tableFooterView = UIView()
like image 21
Danielle Cohen Avatar answered Nov 15 '22 22:11

Danielle Cohen