因此,首先定义您的协议:
protocol SpotDataSource {
var spots: [Spot] { get }
var title: String { get }
}
然后定义两个类以符合该协议:
class PersonalSpotDataSource: SpotDataSource {
let spots = [Spot(name: "foo"), Spot(name: "bar")]
let title = "Foobar"
}
class ExploreSpotDataSource: SpotDataSource {
let spots = [Spot(name: "baz"), Spot(name: "qux")]
let title = "Bazqux"
}
显然,您的这些实现将比上面的更复杂,但这说明了基本思想。
无论如何,完成后,定义
UITableViewDataSource
使用此协议的:
class SpotTableViewDataSource: NSObject {
let spotDataSource: SpotDataSource
init(spotDataSource: SpotDataSource) {
self.spotDataSource = spotDataSource
super.init()
}
}
extension SpotTableViewDataSource: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return spotDataSource.spots.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SpotCell", for: indexPath) as! SpotCell
cell.spotLabel.text = spotDataSource.spots[indexPath.row].name
return cell
}
}
最后,定义表视图控制器以使用
UITableViewDataSource
不管怎么说
SpotDataSource
您需要:
class PersonalSpotTableViewController: UITableViewController {
private let dataSource = SpotTableViewDataSource(spotDataSource: PersonalSpotDataSource())
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = dataSource
}
}
注意,在该表视图控制器类中,我定义了要存储的数据源属性,因为表视图控制器不保留对其数据源和/或委托的强引用,但我们希望它保持不变。
显然,如果我们能把
UITableViewDataSource
中的方法
SpotDataSource
协议的默认实现,但遗憾的是我们不能。所以您必须创建符合
UITableViewDataSource
并使用
SpotDataSource
协议,如上所述。