9

I'm incurred in a little problem customizing my cell;

enter image description here

as you can see the separator line do not reach the left border of the cell, and a I'd like to do it. I found these:

Can anyone help me to translate in swift code?

Community
  • 1
  • 1
Fabio Cenni
  • 841
  • 3
  • 16
  • 30

5 Answers5

14

For conversion of the answer (Separator lines for UITableViewCellStyleSubtitle cells not taking the full width) in Swift

enter image description here

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("yourcell") as! UITableViewCell

    if (cell.respondsToSelector("setPreservesSuperviewLayoutMargins:")){
        cell.layoutMargins = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false
    }
}

By Checking version number :

let floatVersion = (UIDevice.currentDevice().systemVersion as NSString).floatValue

if (floatVersion > 8.0){
    cell.layoutMargins = UIEdgeInsetsZero
    cell.preservesSuperviewLayoutMargins = false
}
Community
  • 1
  • 1
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
  • @FabioCelli did you added separator custom insets to 0? – Ashish Kakkad Aug 24 '15 at 09:54
  • I had already do it, but did not work; now closing and reopening Xcode works (???). – Fabio Cenni Aug 24 '15 at 10:01
  • @FabioCelli That's good. You have to just convert my obj-c answer to swift then it should work well :) Anyways happy to hear that it is working. – Ashish Kakkad Aug 24 '15 at 10:03
  • @iAshish, FYI, Apple doesn't want us to use respondToSelector anymore, you can find information about it in the stateoftheunion video from the WWDC – Loegic Aug 24 '15 at 12:33
  • @Loegic Ok, I we can check by the version number because iOS 7 does not have preservesSuperviewLayoutMargins. Updated the answer. – Ashish Kakkad Aug 24 '15 at 12:40
  • @Loegic can you give me the link in that they have written that you can not use the `respondToSelector` ? – Ashish Kakkad Aug 24 '15 at 12:45
  • @iAshish You can watch it here (Swift 2 improvement) https://developer.apple.com/videos/wwdc/2015/?id=102 and I believe Natashatherobot spoke about it on her blog http://natashatherobot.com – Loegic Aug 25 '15 at 08:19
  • @iAshish I found it, it's a new API called availability checking (#available()), implement it this way : The following code snippet is copied from the WWDC2015 Session 106 What's New in Swift The Old Approach: @ IBOutlet var dropButton: NSButton! override func awakeFromNib() { if dropButton.respondsToSelector("setSpringLoaded:") { dropButton.springLoaded = true } } The Better Approach: @ IBOutlet var dropButton: NSButton! override func awakeFromNib() { if #available(OSX 10.10.3, *) { dropButton.springLoaded = true } } – Loegic Aug 25 '15 at 08:27
  • @Loegic Ok. I will check. Thanks for the links :) – Ashish Kakkad Aug 25 '15 at 13:46
7

Swift 3:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if (cell.responds(to: #selector(setter: UITableViewCell.separatorInset))) {
        cell.separatorInset = UIEdgeInsets.zero
    }

    if (cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins))) {
        cell.preservesSuperviewLayoutMargins = false
    }

    if (cell.responds(to: #selector(setter: UIView.layoutMargins))) {
        cell.layoutMargins = UIEdgeInsets.zero
    }
}
Lukas Batteau
  • 2,473
  • 1
  • 24
  • 16
5

This is old version and it doesn't work anymore.

cell.layoutMargins = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false

You can use this (for swift, in Objective C you can call the same thing)

cell.separatorInset = UIEdgeInsets.zero
congzing
  • 51
  • 1
  • 2
3

the second link should work.

swift version:

if cell.respondsToSelector(Selector("setSeparatorInset:")) {
    cell.separatorInset = UIEdgeInsetsZero
}

if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
    cell.preservesSuperviewLayoutMargins = false
}

if cell.respondsToSelector(Selector("setLayoutMargins:")) {
    cell.layoutMargins = UIEdgeInsetsZero
}
Ypy
  • 56
  • 5
2

Add these lines in your cellForRowAtIndePath method:

    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setLayoutMargins:") {
        cell.layoutMargins = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
        cell.preservesSuperviewLayoutMargins = false
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Rashad
  • 11,057
  • 4
  • 45
  • 73