Oleg Dvoreasin Oleg Dvoreasin - 3 months ago 48
Swift Question

Custom separator in tableView

I have my own separator in .jpg and would like to use it instead of default one in TableView, but the only thing I get is that it disappears.

Swift 3.0:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "musicCell", for: indexPath) as! SongTableViewCell

let color = UIColor(patternImage: UIImage(named:"Separator.jpg")!)
let separator = tableView.separatorColor(color)

tableView.tableFooterView = UIView(frame: .zero)
tableView.backgroundView = UIImageView(image: UIImage(named: "bckgrd.jpg"))
cell.backgroundColor = UIColor.clear

//tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
//tableView.separatorColor = UIColor(patternImage: UIImage(named: "Separator.jpg"))

This is one of the ways I've tried to implement. Is there other way to do that?


You need to set separator style also not color only. Set separator style to single line and make sure that your Separator.jpg image looks like single line and you are getting color by let color = UIColor(patternImage: UIImage(named:"Separator.jpg")!) this statement!

you can set seperator style something like,

 tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine

P.S : If you are checking this on simulator then check it in full resolution (i.e. cmd + 1 or in simulator window - > scale - > 100%), becasue sometime actually there is no problem but because of smaller scale you can't see it

Update :

Another way : Set SeparatorStyle to None from interface builder or by code like,

   tableView.separatorStyle = UITableViewCellSeparatorStyle.None

then you can add seperator image(imageview of height 1 pixel or 2 pixel) to cell at bottom in content view of cell by code or interface builder like,

   let seperatorImageView = UIImageView.init(image: UIImage.init(named: "yourseperatorimg.jpg"))
    seperatorImageView.frame = CGRectMake(0, cell.contentView.frame.size.height - 1.0,  cell.contentView.frame.size.width, 1)