Skip to content

Instantly share code, notes, and snippets.

@codetalks-new
Last active August 29, 2015 14:22
Show Gist options
  • Save codetalks-new/cbd5953dea41d20c833e to your computer and use it in GitHub Desktop.
Save codetalks-new/cbd5953dea41d20c833e to your computer and use it in GitHub Desktop.
//
// GenericDataSource.swift
//
// Created by Haizhen Lee on 15/5/27.
// Copyright (c) 2015年 banxi1988. All rights reserved.
//
import Foundation
class GenericDataSource<T>:SimpleDataSourceBridge{
private var items = [T]()
init(items:[T]){
self.items = items
}
func updateItems(items:[T]){
self.items = items
}
func itemAtIndexPath(indexPath:NSIndexPath) -> T{
return items[indexPath.row]
}
override func numberOfRows() -> Int {
return self.items.count
}
}
//
// SimpleDataSourceBridge.swift
// Youjia
//
// Created by Haizhen Lee on 15/5/27.
// Copyright (c) 2015年 banxi1988. All rights reserved.
//
import UIKit
class SimpleDataSourceBridge:NSObject,UITableViewDataSource,UICollectionViewDataSource{
// MARK: UITableViewDataSource
final func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
final func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfRows()
}
final func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(self.dynamicType.reuseIdentifier, forIndexPath: indexPath) as! UITableViewCell
configureTableViewCell(cell, atIndexPath: indexPath)
return cell
}
// MARK: UICollectionViewDataSource
final func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
final func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return numberOfRows()
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
final func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.dynamicType.reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell
configureCollectionViewCell(cell, atIndexPath: indexPath)
return cell
}
// MARK : Helper
func configureCollectionViewCell(cell:UICollectionViewCell,atIndexPath indexPath:NSIndexPath){
}
func configureTableViewCell(cell:UITableViewCell,atIndexPath indexPath:NSIndexPath){
}
func numberOfRows() -> Int {
return 0
}
class var reuseIdentifier:String{
return "cell"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment