Change tableView separator insets
Asked Answered
S

5

10

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?

Seavey answered 24/8, 2015 at 9:22 Comment(0)
K
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
}
Kentigera answered 24/8, 2015 at 9:29 Comment(9)
@FabioCelli did you added separator custom insets to 0?Kentigera
I had already do it, but did not work; now closing and reopening Xcode works (???).Seavey
@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.Kentigera
@iAshish, FYI, Apple doesn't want us to use respondToSelector anymore, you can find information about it in the stateoftheunion video from the WWDCTelemechanics
@Telemechanics Ok, I we can check by the version number because iOS 7 does not have preservesSuperviewLayoutMargins. Updated the answer.Kentigera
@Telemechanics can you give me the link in that they have written that you can not use the respondToSelector ?Kentigera
@iAshish You can watch it here (Swift 2 improvement) developer.apple.com/videos/wwdc/2015/?id=102 and I believe Natashatherobot spoke about it on her blog natashatherobot.comTelemechanics
@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 } }Telemechanics
@Telemechanics Ok. I will check. Thanks for the links :)Kentigera
R
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
    }
}
Roily answered 20/10, 2017 at 19:25 Comment(0)
U
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
Urissa answered 30/8, 2017 at 3:22 Comment(0)
H
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
}
Homiletic answered 24/8, 2015 at 9:28 Comment(0)
U
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
    }
Uniformed answered 24/8, 2015 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.