Appspotlist is a simple ,clean iOS App demonstrating how to make a custom datasource with generic datamodel type
Simply download or clone this project open in Xcode 9.0 or above hit Run button and there you go
Here i have used two libraries SVProgressHUD and SDWebImage for Loading and Image downloading respectively using CocoaPods
Add the following entry to your Podfile:
pod 'SVProgressHUD', :git => 'https://github.com/SVProgressHUD/SVProgressHUD.git'
pod 'SDWebImage', '~> 4.0'
Then run pod install
.
To make our controller clean and simple , i have used a separate datasource class for handling UITableViewDataSource delegate methods and here this custom datasource class is generic means depending on your datamodeltype you can use this class in your controller
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
}
}
Happy Coding