Swift 2:UITableViewDataSource协议扩展 [英] Swift 2: UITableViewDataSource protocol extension
问题描述
我一直在玩协议扩展,但遇到了问题.也许我想实现的目标无法实现.我有这个游乐场:
I have been playing around with protocol extensions and I have a problem. Maybe what I want to achieve can’t be done. I have this playground:
//: Playground - noun: a place where people can play
import UIKit
protocol ArrayContainer {
typealias T
var array: [T] { get }
}
class MyViewController: UIViewController, ArrayContainer, UITableViewDataSource {
typealias T = String
var array = ["I am", "an Array"]
}
extension UITableViewDataSource where Self: ArrayContainer {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Whatever
return UITableViewCell()
}
}
这就是我所拥有的和我想要的:
This is what I have and what I want:
- 我有一个协议
ArrayContainer
,它只有一个类型别名和一个数组,其中包含这种类型别名的对象 - 我有一个
UITableViewDataSource
协议扩展名,当该类符合ArrayController
协议时使用.这只是返回数组的项目数作为行数.cellForRowAtIndexPath
方法的实现不是很好,但这不是问题. - 我有一个名为
MyViewController
的UIViewController
子类,它实现了两种协议.
- I have a protocol
ArrayContainer
that just has a typealias and a array which contains objects of this typealias type - I have a protocol extension of
UITableViewDataSource
to be used when the class conforms with the ArrayController
protocol. This simply returns the number of items of the array as number of rows. ThecellForRowAtIndexPath
method is not well implemented, but it is not the problem. - I have a
UIViewController
subclass called MyViewController
which implements both protocols.
问题是编译器抱怨是因为MyViewController不符合UITableViewDataSource
,但是据我所知,它应该由UITableViewDataSource扩展覆盖.我在这里想念什么吗?还是无法扩展Objective-C协议?
The problem is that the compiler complains because MyViewController doesn’t conforms with UITableViewDataSource
but, as far as i know, it should be covered by the UITableViewDataSource extension. Am I missing something here? or maybe Objective-C protocols can not be extended?
推荐答案
我知道现在答复有点晚了,您可能甚至没有在寻找这个答案,但是我碰到了这个确切的问题,需要一个真实的世界解决方案".您可以在类中实现UITableViewDataSource方法,然后立即将工作交给协议扩展,如下例所示.如果swift做出了不再需要的改进,则可以很容易地更改回原始帖子中的代码.
I know it's a bit late to respond, and you may not even be looking for this answer, but I just came across this exact issue and needed a real world "solution". You can implement the UITableViewDataSource methods in the class and then immediately hand off the work to the protocol extension like the example below. If swift makes improvements that no longer require this, it's simple to change back to the code in your original post.
//: Playground - noun: a place where people can play
import UIKit
protocol ArrayContainer {
associatedtype T
var array: [T] { get }
}
class MyViewController: UIViewController, ArrayContainer, UITableViewDataSource {
typealias T = String
var array = ["I am", "an Array"]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.internal_numberOfSectionsInTableView(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.internal_tableView(tableView, numberOfRowsInSection: section)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return self.internal_tableView(tableView, cellForRowAtIndexPath: indexPath)
}
}
extension UITableViewDataSource where Self: ArrayContainer {
func internal_numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func internal_tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func internal_tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Whatever
return UITableViewCell()
}
}
这篇关于Swift 2:UITableViewDataSource协议扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!