简体   繁体   English

Xcode 8 / Swift 3:didSelectRowAt vs didSelectRowAtIndexPath

[英]Xcode 8 / Swift 3: didSelectRowAt vs didSelectRowAtIndexPath

Something really weird is going on after our Swift 3 migration. 我们的Swift 3迁移之后会发生一些非常奇怪的事情。 We have two view controllers, both of them implement UITableViewDelegate and both of them implement public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 我们有两个视图控制器,它们都实现了UITableViewDelegate ,它们都实现了public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

However only in one of them the actual method is called. 但是只在其中一个中调用实际方法。

If I change in the one that doesn't work public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) to public func tableView(_ tableView: UITableView, didSelectRowAtIndexPath: IndexPath) (notice the Swift 2.2 signature) then they both work. 如果我改变了一个不起作用的public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)public func tableView(_ tableView: UITableView, didSelectRowAtIndexPath: IndexPath) (注意Swift 2.2签名)那么他们都工作。

Both view controllers are Swift classes, so I am not sure what the heck is going on. 两个视图控制器都是Swift类,所以我不确定到底发生了什么。 I am pretty sure it might be a Objective-C vs Swift interoperability issue, but our whole project is written in Swift, so that's why it's hard to figure out what is causing this. 我很确定它可能是一个Objective-C vs Swift互操作性问题,但我们的整个项目是用Swift编写的,所以这就是为什么很难弄清楚导致这个问题的原因。

Any help is appreciated. 任何帮助表示赞赏。 Thank you. 谢谢。

for Swift 3.0, use 对于Swift 3.0,请使用

override  func tableView(_ tableView: UITableView, didSelectRowAt
 indexPath: IndexPath){

  //your code...  


 }

I encountered a similar problem. 我遇到了类似的问题。 My problem was caused by having a superclass that adopted the UITableViewDelegate and UITableViewDataSource protocols, and then implementing the actual methods in a subclass. 我的问题是由于一个超类采用了UITableViewDelegate和UITableViewDataSource协议,然后在子类中实现了实际的方法。

What I gather is that because UITableViewDelegate and UITableViewDataSource are objective-c protocols, they must be adopted directly by the class implementing these functions. 我收集的是因为UITableViewDelegate和UITableViewDataSource是objective-c协议,它们必须由实现这些功能的类直接采用。 Otherwise the Swift function signatures will not be properly mapped to the objective-c function signatures (not sure why this is the case). 否则,Swift函数签名将无法正确映射到objective-c函数签名(不确定为什么会出现这种情况)。

In Swift versions prior to 3.0, the underlying objective-c function signatures matched the signatures in the Swift UITableViewDelegate and UITableViewDataSource protocols. 在3.0之前的Swift版本中,底层的objective-c函数签名与Swift UITableViewDelegate和UITableViewDataSource协议中的签名相匹配。 Therefore prior to 3.0 it seems to have worked fine to have a superclass adopt these protocols. 因此,在3.0之前,超类采用这些协议似乎工作得很好。 However as of Swift 3.0 these signatures are no longer a match. 但是从Swift 3.0开始,这些签名不再匹配。 It seems that to have the new-style signature properly mapped to the underlying objective-c signature, your class must directly adopt the UITableViewDelegate and UITableViewDataSource protocols. 似乎要将新样式的签名正确映射到底层的objective-c签名,您的类必须直接采用UITableViewDelegate和UITableViewDataSource协议。

Therefore in Swift 3.0 and later, if you do not directly adopt the UITableViewDelegate and UITableViewDataSource protocols then your function signatures must match the old-style underlying objective-c signatures in order for your functions to be called correctly. 因此,在Swift 3.0及更高版本中,如果您不直接采用UITableViewDelegate和UITableViewDataSource协议,那么您的函数签名必须与旧式底层Objective-c签名匹配,才能正确调用您的函数。

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

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