gpt4 book ai didi

ios - 在 Swift 中设置带边距的 UITableViewCell 框架

转载 作者:搜寻专家 更新时间:2023-10-31 22:05:28 24 4
gpt4 key购买 nike

我正在尝试制作一个带有左右边距/插图的 UITableViewCell。我过去曾使用以下代码使用 Objective-C 完成此操作:

- (void)setFrame:(CGRect)frame {
frame.origin.x += 25;
frame.size.width -= 2 * 25;
[super setFrame:frame];
}

此代码不会直接转换为 Swift,因为现在我必须重写公共(public)框架变量,但将此代码放在该变量的 setter 中没有任何效果。我如何使用此方法或 Swift 中的类似技术来显示边距?

这是我尝试过的 Swift:

private var otherFrame: CGRect = CGRectZero
override public var frame: CGRect {
get {
return otherFrame
}
set {
otherFrame = frame
otherFrame.origin.x += 25
otherFrame.size.width -= 2 * 25
}
}

最佳答案

Objective-C 代码的直接翻译是:

override var frame: CGRect {
get {
return super.frame
}
set {
var frame = newValue
frame.origin.x += 25
frame.size.width -= 2 * 25

super.frame = frame
}
}

不需要额外的属性。您在 setter 中使用隐式值 newValue

关于ios - 在 Swift 中设置带边距的 UITableViewCell 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42094533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com