在使用代码删除 tableview 中的额外分隔线时,我在 viewcontroller 中为 tableview 创建了一个导出,然后设置了
self.tableview.tableFooterView = UIView()
在检查类 UIView 时,默认的初始化程序有 frame 参数,
class UIView : UIResponder, NSCoding, UIAppearance, NSObjectProtocol, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace {
class func layerClass() -> AnyClass // default is [CALayer class]. Used when creating the underlying layer for the view.
init(frame: CGRect) // default initializer
var userInteractionEnabled: Bool // default is YES. if set to NO, user events (touch, keys) are ignored and removed from the event queue.
var tag: Int // default is 0
var layer: CALayer { get } // returns view's layer. Will always return a non-nil value. view is layer's delegate
}
使用,
self.tableview.tableFooterView = UIView(frame: CGRectZero)
也有效。当 UIView 类(它确实继承自许多其他类)中的默认初始值设定项不同时,为什么第一个方法有效并且不报错?删除分隔符的两条线有何不同(如果它们不同),哪一条更有效并且应该使用?
最佳答案
UIView
从 NSObject
继承了 init()
,这就是为什么它是有效的并且不会给出任何错误。 Cocoa(Touch) 中与 Objective-C 一起工作的每个类都继承自此类(NSProxy
除外)。很明显,UIView
的 frame
属性默认为 CGRectZero
,这就是它仍然有效的原因。
我会说您总是选择 init(frame:)
,因为它是 UIView
的Designated Initializer。它为读者提供了更清晰的意图。 通过使用 init()
,您无法确定所有属性的设置是否比使用 init(frame:)
更正确。
更新
在做了一些小实验后,我发现 UIView.init()
通过传递 CGRectZero
调用 UIView.init(frame:)
>。所以,我会说两者完全等同。但是,我仍然建议您使用 init(frame:)
,因为它可以向读者表达更清晰的意图。但这取决于您的风格。
代码如下:
@interface MyView : UIView
@end
@implementation MyView
- (instancetype)init; {
self = [super init];
if (self) {
NSLog(@"%s", __FUNCTION__);
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSLog(@"%s | %@", __FUNCTION__, NSStringFromCGRect(frame));
}
return self;
}
@end
然后通过调用 [[MyView alloc] init];
它打印:
-[MyView initWithFrame:] | {{0, 0}, {0, 0}}
-[MyView init]
更新
这是 Swift 版本:
class MyView: UIView {
override init() {
super.init()
println(__FUNCTION__)
}
override init(frame: CGRect) {
super.init(frame: frame)
println("\(__FUNCTION__) | \(frame)")
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
println(__FUNCTION__)
}
}
然后您只需在代码中的某处调用 MyView()
。
关于ios - 消除 Swift 中 UITableView 中空行的额外分隔线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28507082/