更改 tableView 分隔符插入

Change tableView separator insets

我在自定义手机时遇到了一点问题;

如您所见,分隔线没有到达单元格的左边框,我想这样做。我找到了这些:

谁能帮我翻译成 swift 代码?

第二个 link 应该可以。

swift 版本:

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

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

if cell.respondsToSelector(Selector("setLayoutMargins:")) {
    cell.layoutMargins = UIEdgeInsetsZero
}

用于Swift

中答案()的转换

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
    }
}

通过检查版本号:

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

if (floatVersion > 8.0){
    cell.layoutMargins = UIEdgeInsetsZero
    cell.preservesSuperviewLayoutMargins = false
}

在您的 cellForRowAtIndePath 方法中添加这些行:

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

这是旧版本,不再有效。

cell.layoutMargins = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false

你可以使用这个(对于 swift,在 Objective C 你可以调用同样的东西)

cell.separatorInset = UIEdgeInsets.zero

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
    }
}