ios Swift - UITableView 设置分隔符样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27760212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift - UITableView set separator style
提问by TimWhiting
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.
如何在 Swift 中设置 UITableView 的分隔符样式?我想去掉分隔符,以便表格视图中的单元格之间没有灰线。任何建议将不胜感激。以下是我已经尝试过的。
I have found this method in objective-C
我在objective-C中找到了这个方法
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]
I tried my best to translate this to swift with
我尽力将其翻译为 swift
self.tableView.setSeparatorStyle = UITableViewCellSeparatorStyle.None
But this presents the error
但这出现了错误
'(UITableView, numberOfRowsInSection: Int) -> Int' does not have a member named 'setSeparatorStyle'
In the apple documentation it gives these declarations in objective-C
在苹果文档中,它在 Objective-C 中给出了这些声明
typedef enum : NSInteger {
UITableViewCellSeparatorStyleNone ,
UITableViewCellSeparatorStyleSingleLine ,
UITableViewCellSeparatorStyleSingleLineEtched
} UITableViewCellSeparatorStyle;
So I tried again to translate it to Swift using
所以我再次尝试使用
override func tableView(tableView: UITableView, setSeparatorStyle style: NSInteger) -> NSInteger {
return UITableViewCellSeparatorStyleNone
}
But this just throws up more errors, "Use of unresolved identifier 'UITableViewCellSeparatorStyleNone"
但这只会引发更多错误,“使用未解析的标识符‘UITableViewCellSeparatorStyleNone”
回答by wander
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:
为了解释为什么不能setSeparatorStyle
在 Swift 中调用,我需要解释该方法在 Objective-C 中的来源。如果您在标题 ( UITableView.h
) 中搜索它,您将找不到它。唯一声明的是:
@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle;
setSeparatorStyle
is the setter automatically generated for a property in Objective-C. Let's say you declare a property in Objective-C, like this:
setSeparatorStyle
是为 Objective-C 中的属性自动生成的 setter。假设您在 Objective-C 中声明了一个属性,如下所示:
@interface SomeClass: NSObject
@property (nonatomic) id someProperty;
@end
Objective-C will automatically generate a setter and getter method for that property, with the getter being the name of the property, and the setter being the name with set
prefixed.
Objective-C 会自动为该属性生成 setter 和 getter 方法,getter 是属性的名称,setter 是带set
前缀的名称。
So you can call the getter like this:
所以你可以像这样调用getter:
[object someProperty]
[object someProperty]
And you can set the property like this:
[object setSomeProperty:newValue]
您可以像这样设置属性:
[object setSomeProperty:newValue]
Now, that's all great, but there's a shorter way to call the setter and getter, namely dot syntax, which looks like this:
现在,这一切都很好,但是有一种更短的方法来调用 setter 和 getter,即dot syntax,如下所示:
object.someProperty = newValue; // setter
object.someProperty = newValue; // setter
And the getter:
和吸气剂:
object.someProperty // getter
object.someProperty // getter
Now in Swift, this implicitgeneration of a setter in this setSomeProperty
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.
现在在 Swift 中,这种隐式生成的 settersetSomeProperty
不再存在。这一直是 Objective-C 的一个奇怪的怪癖,所以 Swift 引入了一种统一的方式来设置和获取属性。在 Swift 中,只有一种设置和获取属性的方法,它使用点语法,这与 Objective-C 中的点语法相同。
So to actually answer your question, you need to remove the set
part in setSeparatorStyle
, and this would be your new code:
因此,要实际回答您的问题,您需要删除 中的set
部分setSeparatorStyle
,这将是您的新代码:
self.tableview.separatorStyle = UITableViewCellSeparatorStyle.None
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
通常你不必写self
,Swift 足够聪明,可以意识到self.tableView
如果tableView
作用域中只有一个命名的变量你想使用,所以你可以写
tableview.separatorStyle = UITableViewCellSeparatorStyle.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
.
Swift 甚至足够聪明,它意识到赋值运算符(等号,=
)后面的内容可能是分隔符样式,因此您只需编写.None
.
tableview.separatorStyle = .None
回答by fandro
For those who are looking for Swift 2 and 3 notation :
对于那些正在寻找 Swift 2 和 3 符号的人:
tableView.separatorStyle = UITableViewCellSeparatorStyle.None;
回答by AmyNguyen
I used :
我用了 :
tableview.separatorStyle = .None
回答by dengST30
In Swift 5
在斯威夫特 5
tableview.separatorStyle = UITableViewCell.SeparatorStyle.none
回答by ScarletEnvy
Look under the:
看看下面的:
Attributes inspector > table view > separator
This should help you change the separator style or remove it completely
Attributes inspector > table view > separator
这应该可以帮助您更改分隔符样式或将其完全删除