Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 模拟UITableViewCellStyleValue1附件类型detailTextLabel间距与自动布局?_Ios_Uitableview_Autolayout - Fatal编程技术网

Ios 模拟UITableViewCellStyleValue1附件类型detailTextLabel间距与自动布局?

Ios 模拟UITableViewCellStyleValue1附件类型detailTextLabel间距与自动布局?,ios,uitableview,autolayout,Ios,Uitableview,Autolayout,将默认UITableViewCell与UITableViewCellStyleValue1一起使用时,detailTextLabel会知道accessoryType。标签与单元格边缘之间的距离与标签与accessoryType视图之间的距离不同。使用自定义UITableViewCell子类时,两个距离相等。这看起来不像默认实现那么好 这有点难以解释,所以这里有一个屏幕截图: 前两个单元格是默认的UITableViewCellStyleValue1 UITableViewCell。 单元格3和4

将默认UITableViewCell与UITableViewCellStyleValue1一起使用时,detailTextLabel会知道accessoryType。标签与单元格边缘之间的距离与标签与accessoryType视图之间的距离不同。使用自定义UITableViewCell子类时,两个距离相等。这看起来不像默认实现那么好

这有点难以解释,所以这里有一个屏幕截图:

前两个单元格是默认的UITableViewCellStyleValue1 UITableViewCell。
单元格3和4是使用
H:[label]-16-|
约束设置的自定义单元格。
单元格5和6是使用
Trailing-0-TrailingMargin
约束设置的自定义单元格


我想为我自己的单元格获取默认单元格的行为。如何使用自动布局实现这一点

iOS实际上会移动
contentView
,因此解决方案就是简单地调整contentView和标签之间的距离:

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    /* ... */
    trailingDetailConstraint = NSLayoutConstraint(item: contentView, attribute: .Right, relatedBy: .Equal, toItem: rightLabel, attribute: .Right, multiplier: 1, constant: 15)
    contentView.addConstraint(trailingDetailConstraint)
}

override var accessoryType: UITableViewCellAccessoryType {
    didSet {
        if accessoryType == .None {
            trailingDetailConstraint.constant = 15
        }
        else {
            trailingDetailConstraint.constant = 0
        }
        layoutIfNeeded()
    }
}

override var editingAccessoryType: UITableViewCellAccessoryType {
    didSet {
        if accessoryType == .None {
            trailingDetailConstraint.constant = 15
        }
        else {
            trailingDetailConstraint.constant = 0
        }
        layoutIfNeeded()
    }
}

这太棒了。你在Objective-C中有相同的例子吗?