8

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!

bwash70
  • 1,135
  • 2
  • 11
  • 24
  • Did you check that the background color is correct for your custom Cells? – Matteo Piombo May 11 '15 at 04:47
  • Hey Matteo - I've updated the question to show how the cells are being formatted. Also, If I remove that code and have standard cells they show grey lines. I'm sure these aren't separator lines because they're usually aligned to the right aren't they? I'm stumped. There must be something I've done because my tableview code is saved as a snippet. I've just restarted XCode as well which didn't work. – bwash70 May 11 '15 at 05:00
  • Isn't it the default tint color that we see? As a test try also setting the contentView's bg color: `cell.contentView.backgroundColor = ...` – Matteo Piombo May 11 '15 at 05:18
  • Can you clarify which tint color you're referring to? The cells are set to blue with white text, the tint color for the tabbar and navigation bar are set to white. I've set the `cell.contentView.backgroundColor` but still the same. – bwash70 May 11 '15 at 05:36
  • Just trying to figure out where that color comes from. It seemed the default tint color used by IB. It appears you have separator insets, what if you set them to 0?: `cell.separatorInset = UIEdgeInsetsZero` – Matteo Piombo May 11 '15 at 05:42
  • @bwash70, can you show me your custom cell with layout in your xib. if you not have any consern – Jatin Patel - JP May 11 '15 at 05:45
  • @None It isn't a custom cell. It's a standard `UITableViewCell` with blue background set in `cellForRowAtIndex`. – bwash70 May 11 '15 at 05:51
  • @MatteoPiombo I have tried both `self.tableView.separatorInset = UIEdgeInsetsZero` and `cell.separatorInset = UIEdgeInsetsZero` but still the same. – bwash70 May 11 '15 at 05:53
  • Mmm... but it should. From your image the line is not edge to edge. Try setting a breakpoint at your cellForRowAtIndexPath and see if it get called from the table you intend to. – Matteo Piombo May 11 '15 at 06:00
  • I've just updated the question to show a screenshot with separator insets but these additional lines still show. – bwash70 May 11 '15 at 06:09
  • @MatteoPiombo I've set the breakpoint and it is loading from the expected location. I've also removed all custom code from the `cellForRowAtIndex` except `cell.textLabel?.text = "Hello"` and it still shows the extra line. – bwash70 May 11 '15 at 06:15
  • Subclassing UITableViewCell without overriding anything doesn't fix the issue. I'll subclass tableview cell and replace the content view and see what happens. I've created 1000 tables before and never seen this! – bwash70 May 11 '15 at 06:27
  • 1
    Why you are using let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell? When you cell is not custom cell. Also self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") is not required. Try this let cell : UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cell"). – Amit89 May 11 '15 at 06:31
  • Copy and pasted your code as much as possible, and still can't reproduce your problem :-/ Do you set rowHeight or something? – Matteo Piombo May 11 '15 at 06:46
  • @Amit89 Nailed it... do you want to add that as the answer and I'll mark as correct. I removed both `let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell?` and `self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")` and added `let cell : UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cell")`. My TableView snippet to generate the code was created from a custom cell. Thank you! – bwash70 May 11 '15 at 07:02
  • @MatteoPiombo Thanks for all your efforts! Not sure why you can't replicate it, I would imaging that this happens anytime you use the custom cell code without using a custom cell... anyway, thanks Matteo - very much appreciated! – bwash70 May 11 '15 at 07:03
  • @Amit89 In my other tableviews i have `var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell if (cell != "") { cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell") }`. Can you confirm whether I need the `if(cell != "")` part – bwash70 May 11 '15 at 07:06
  • Mostly not required, since you are not using custom cell's. – Amit89 May 11 '15 at 07:14

5 Answers5

7

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

Community
  • 1
  • 1
Fatti Khan
  • 1,543
  • 1
  • 11
  • 19
  • Hey - I'm not using storyboards and have already tried using tableview.separatorStyle = UITableViewCellSeparatorStyle.None. I've seen the link you posted and that is for extra lines below populated cells in the tableview - which isn't happening to me. – bwash70 May 11 '15 at 04:31
7

in your viewDidLoad() function, add this:

tableView.tableFooterView = UIView()
Danielle Cohen
  • 629
  • 8
  • 5
  • Thanks Danielle. It helped me... I personally feel that Apple should add this as default. Most of the time we set table view with certain height & if NO data is available then it shows blank separator lines which looks weird. – Ameer Sep 05 '18 at 07:09
4

Since you are not using custom cell's you need to do the following.

Create cell's as let cell : UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cell")

and remove self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

Amit89
  • 3,000
  • 1
  • 19
  • 27
1

Here's a little additional information about eliminating the spurious separators that might be helpful. The solution that the originator said worked was this one that creates a new cell:

let cell : UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cell")

But the originator did not say how he worked this into his code, whose original version used the two argument version of dequeueReusableCellWithIdentifier:

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

I experienced the same problem of spurious separators and found that using the single argument version of dequeueReusableCellWithIdentifier also fixes the problem:

let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
Percy
  • 35
  • 8
0

I ran into same issue recently, and finally fixed by this in cell's init:

self.contentView.backgroundColor = UIColor.AppBgColor;
self.backgroundColor = UIColor.AppBgColor;

If you look closely in View Hierarchy, the cell's background and it's contentView's background are not same, which leads to this issue.

enter image description here

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
teoking
  • 127
  • 7