简体   繁体   English

Swift - UITableView 设置分隔符样式

[英]Swift - UITableView set separator style

How do i set the Separator Style of my UITableView in Swift?如何在 Swift 中设置 UITableView 的分隔符样式? 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.任何建议将不胜感激。以下是我已经尝试过的。

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”

To explain why you can't call setSeparatorStyle in Swift, I need to explain where that method comes from in Objective-C.为了解释为什么不能在 Swift 中调用setSeparatorStyle ,我需要解释该方法在 Objective-C 中的来源。 If you search for it in the header ( UITableView.h ) you won't find it.如果您在标题 ( UITableView.h ) 中搜索它,您将找不到它。 The only thing declared is this:唯一声明的是:

@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle;

setSeparatorStyle is the setter automatically generated for a property in Objective-C. setSeparatorStyle是为 Objective-C 中的属性自动生成的 setter。 Let's say you declare a property in Objective-C, like this:假设您在 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]

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

And the getter:和吸气剂:

object.someProperty // getter

Now in Swift, this implicit generation of a setter in this setSomeProperty way doesn't exist anymore.现在在 Swift 中,这种以setSomeProperty方式隐式生成的 setter 不再存在。 It had always been a weird quirk of Objective-C, so Swift introduces a unified way to set and get properties.这一直是 Objective-C 的一个奇怪的怪癖,所以 Swift 引入了一种统一的方式来设置和获取属性。 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 中,只有一种设置和获取属性的方法,它使用点语法,这与 Objective-C 中的点语法相同。

So to actually answer your question, you need to remove the set part in setSeparatorStyle , and this would be your new code:因此,要实际回答您的问题,您需要删除setSeparatorStyle中的set部分,这将是您的新代码:

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 足够聪明,可以意识到如果范围内只有一个名为tableView变量,你想使用self.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

对于那些正在寻找 Swift 2 和 3 符号的人:

tableView.separatorStyle = UITableViewCellSeparatorStyle.None;

我用了 :

tableview.separatorStyle = .None

在斯威夫特 5

tableview.separatorStyle = UITableViewCell.SeparatorStyle.none

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这应该可以帮助您更改分隔符样式或将其完全删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM