Skip to content

Instantly share code, notes, and snippets.

@shaktiprakash099
Created December 7, 2018 11:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaktiprakash099/cb8cf638362fa4575a009108abb04c2e to your computer and use it in GitHub Desktop.
Save shaktiprakash099/cb8cf638362fa4575a009108abb04c2e to your computer and use it in GitHub Desktop.
import UIKit
class TableViewCustomDataSource<Model>: NSObject,UITableViewDataSource{
typealias CellConfigurator = (Model,UITableViewCell)-> Void
var models:[Model]
private let reuseIdentifier:String
private let cellConfigurator: CellConfigurator
init(models:[Model],reuseIdentifier:String,cellConfigurator:@escaping CellConfigurator) {
self.models = models
self.reuseIdentifier = reuseIdentifier
self.cellConfigurator = cellConfigurator
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = models[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
cellConfigurator(model,cell)
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment