ios - swift 2 : UITableViewDataSource protocol extension

标签 ios uitableview swift2 protocol-extension

我一直在研究协议(protocol)扩展,但遇到了问题。也许我想达到的目标无法完成。我有这个 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()
    }   
}

这就是我所拥有的和我想要的:

  • 我有一个协议(protocol) ArrayContainer ,它只有一个类型别名和一个包含此类型别名类型的对象的数组
  • 我有一个 UITableViewDataSource 的协议(protocol)扩展,当类符合 ArrayController 协议(protocol)时使用。这只是将数组的项目数返回为行数。 cellForRowAtIndexPath 方法没有很好地实现,但这不是问题所在。
  • 我有一个名为 MyViewControllerUIViewController 子类,它实现了这两种协议(protocol)。

问题是编译器会提示,因为 MyViewController 不符合 UITableViewDataSource,但据我所知,它应该包含在 UITableViewDataSource 扩展中。我在这里错过了什么吗?或者 Objective-C 协议(protocol)可能无法扩展?

最佳答案

我知道现在回复有点晚,您甚至可能不会寻找这个答案,但我刚遇到这个确切的问题,需要一个现实世界的“解决方案”。您可以在类中实现 UITableViewDataSource 方法,然后立即将工作交给协议(protocol)扩展,如下例所示。如果 swift 进行了不再需要此的改进,则很容易改回原始帖子中的代码。

//: 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()
    }   
}

关于ios - swift 2 : UITableViewDataSource protocol extension,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32582638/

相关文章:

ios - 以编程方式滑动 UITableViewCell?

ios - 有没有办法使用 Swift 中的速率属性来倒带音乐?

objective-c - UIViewController 用于 UITableView tableHeaderView

swift - 如何修复错误 "deviceInputWithDevice is unavailable"?

swift - NSObject 类符合 Swift 中 NSArray 中包含的协议(protocol)

ios - removeObserverWithHandle 似乎无法在 fireBase iOS 中工作

javascript - 优雅地崩溃 ios cordova 应用程序

objective-c - 设置 SOCKS 代理配置后 CFStream 崩溃

ios - 检测按钮被点击 - iOS

ios - 将 UITableView 集成到 iOS 标注注释中