14

How do i set the Separator Style of my UITableView in Swift? I want to get rid of the separator so that there is no grey line between the cells in my table view. Any suggestions would be greatly appreciated.Below is what I have already tried.

I have found this method in objective-C

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone] 

I tried my best to translate this to swift with

self.tableView.setSeparatorStyle = UITableViewCellSeparatorStyle.None 

But this presents the error

'(UITableView, numberOfRowsInSection: Int) -> Int' does not have a member named 'setSeparatorStyle' 

In the apple documentation it gives these declarations in objective-C

typedef enum : NSInteger { UITableViewCellSeparatorStyleNone , UITableViewCellSeparatorStyleSingleLine , UITableViewCellSeparatorStyleSingleLineEtched } UITableViewCellSeparatorStyle; 

So I tried again to translate it to Swift using

 override func tableView(tableView: UITableView, setSeparatorStyle style: NSInteger) -> NSInteger { return UITableViewCellSeparatorStyleNone } 

But this just throws up more errors, "Use of unresolved identifier 'UITableViewCellSeparatorStyleNone"

7 Answers 7

32

To explain why you can't call setSeparatorStyle in Swift, I need to explain where that method comes from in Objective-C. If you search for it in the header (UITableView.h) you won't find it. The only thing declared is this:

@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle; 

setSeparatorStyle is the setter automatically generated for a property in Objective-C. Let's say you declare a property in Objective-C, like this:

@interface SomeClass: NSObject @property (nonatomic) id someProperty; @end 

Objective-C will automatically generate a setter and getter method for that property, with the getter being the name of the property, and the setter being the name with set prefixed.

So you can call the getter like this:

[object someProperty]

And you can set the property like this: [object setSomeProperty:newValue]

Now, that's all great, but there's a shorter way to call the setter and getter, namely dot syntax, which looks like this:

object.someProperty = newValue; // setter

And the getter:

object.someProperty // getter

Now in Swift, this implicit generation of a setter in this setSomeProperty way doesn't exist anymore. It had always been a weird quirk of Objective-C, so Swift introduces a unified way to set and get properties. In Swift, there's only one way to set and get properties and it's using dot syntax, which is identical to the dot syntax in Objective-C.

So to actually answer your question, you need to remove the set part in setSeparatorStyle, and this would be your new code:

self.tableview.separatorStyle = UITableViewCellSeparatorStyle.none 

Usually you don't have to write self, Swift is smart enough to realize you want to use self.tableView if there's only one variable named tableView in scope, so you can write

tableview.separatorStyle = UITableViewCellSeparatorStyle.none 

And Swift is even smart enough to realize that what comes after the assignment operator (the equals sign, =) is probably a separator style, so you can just write .none.

tableview.separatorStyle = .none 
Sign up to request clarification or add additional context in comments.

1 Comment

Great post! In Swift4 .None has been renamed to .none
8

For those who are looking for Swift 2 and 3 notation :

tableView.separatorStyle = UITableViewCellSeparatorStyle.None; 

Comments

7

I used :

tableview.separatorStyle = .None 

Comments

6

In Swift 5

tableview.separatorStyle = UITableViewCell.SeparatorStyle.none 

Comments

3

Look under the:

Attributes inspector > table view > separator This should help you change the separator style or remove it completely

1 Comment

Thanks this is also a valid solution
0

tableView.separatorStyle = .none tableView.separatorStyle = .singleLine

1 Comment

This answer doesn't provide any explanation nor does it improve in any way on the existing answers. It's always best to include an explanation and not just code.
0

Swift 5

We can add UITableView set separator style below.

@IBOutlet weak private var listingTableView: UITableView! { didSet { listingTableView.delegate = self listingTableView.dataSource = self listingTableView.separatorStyle = .singleLine **OR** .none listingTableView.tableFooterView = UIView(frame: .zero) listingTableView.separatorColor = UIColor(hex: "E6E6E6") } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.