Swift - UITableView设置分隔符样式 [英] Swift - UITableView set separator style
问题描述
如何在Swift中设置UITableView的分隔符样式?我想摆脱分隔符,以便在我的表视图中的单元格之间没有灰线。任何建议都会非常感激。我已经尝试过了。
How do i set the Separator Style of my UITableView in Swift? I want to get rid of the separator so that there is no grey line between the cells in my table view. Any suggestions would be greatly appreciated.Below is what I have already tried.
我在Objective-C中找到了这个方法
I have found this method in objective-C
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]
我尽力将其翻译为swift with
I tried my best to translate this to swift with
self.tableView.setSeparatorStyle = UITableViewCellSeparatorStyle.None
但这会出现错误
'(UITableView, numberOfRowsInSection: Int) -> Int' does not have a member named 'setSeparatorStyle'
在苹果文档中,它以客观的方式提供这些声明-C
In the apple documentation it gives these declarations in objective-C
typedef enum : NSInteger {
UITableViewCellSeparatorStyleNone ,
UITableViewCellSeparatorStyleSingleLine ,
UITableViewCellSeparatorStyleSingleLineEtched
} UITableViewCellSeparatorStyle;
所以我再次尝试使用
override func tableView(tableView: UITableView, setSeparatorStyle style: NSInteger) -> NSInteger {
return UITableViewCellSeparatorStyleNone
}
但这只会引发更多错误, 使用未解析的标识符'UITableViewCellSeparatorStyleNone
But this just throws up more errors, "Use of unresolved identifier 'UITableViewCellSeparatorStyleNone"
推荐答案
解释为什么你不能调用 setSeparatorStyle
在Swift中,我需要解释Objective-C中该方法的来源。如果你在标题( UITableView.h
)中搜索它,你将找不到它。唯一声明的是:
To explain why you can't call setSeparatorStyle
in Swift, I need to explain where that method comes from in Objective-C. If you search for it in the header (UITableView.h
) you won't find it. The only thing declared is this:
@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle;
setSeparatorStyle
是为a自动生成的setter Objective-C中的属性。假设您在Objective-C中声明了一个属性,如下所示:
setSeparatorStyle
is the setter automatically generated for a property in Objective-C. Let's say you declare a property in Objective-C, like this:
@interface SomeClass: NSObject
@property (nonatomic) id someProperty;
@end
Objective-C将自动生成一个setter和getter方法对于该属性,getter是getter的名称,setter是名称, set
前缀。
Objective-C will automatically generate a setter and getter method for that property, with the getter being the name of the getter, and the setter being the name with set
prefixed.
所以你可以像这样调用getter:
So you can call the getter like this:
[object someProperty]
您可以像这样设置属性:
[object setSomeProperty:newValue]
And you can set the property like this:
[object setSomeProperty:newValue]
现在,这一切都很棒,但是有一个更短的方式来调用setter和getter,即 dot syntax ,它们看起来像这样:
Now, that's all great, but there's a shorter way to call the setter and getter, namely dot syntax, which looks like this:
object.someproperty = newValue; // setter
吸气剂:
object.someproperty // getter
现在在Swift中,这个隐式在这个<$ c中生成一个setter $ c> setSomePropety 方式不再存在。它一直是Objective-C的奇怪怪癖,因此Swift引入了一种统一的方法来设置和获取属性。 在Swift中,只有一种设置和获取属性的方法,它使用点语法,这与Objective-C中的点语法相同。
Now in Swift, this implicitly generating of a setter in this setSomePropety
way doesn't exist anymore. It had always been a weird quirk of Objective-C, so Swift introduces a unified way to set and get properties. In Swift, there's only one way to set and get properties and it's using dot syntax, which is identical to the dot syntax in Objective-C.
所以要真正回答你的问题,你需要删除 setSeparatorStyle
中的 set
部分,这将是你的新代码:
So to actually answer your question, you need to remove the set
part in setSeparatorStyle
, and this would be your new code:
self.tableview.separatorStyle = UITableViewCellSeparatorStyle.none
通常你不必写 self
,Swift很聪明,意识到你想要使用 self.tableView
如果范围内只有一个名为 tableView
的变量,那么你可以写
Usually you don't have to write self
, Swift is smart enough to realize you want to use self.tableView
if there's only one variable named tableView
in scope, so you can write
tableview.separatorStyle = UITableViewCellSeparatorStyle.none
Swift甚至足够聪明地意识到赋值运算符后面的内容(等号, =
)可能是分隔符样式,所以你可以写 .none
。
And Swift is even smart enough to realize that what comes after the assignment operator (the equals sign, =
) is probably a separator style, so you can just write .none
.
tableview.separatorStyle = .none
这篇关于Swift - UITableView设置分隔符样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!