Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alfianlosari/cf2b8e420a2c81bb49def9dc58a5334a to your computer and use it in GitHub Desktop.
Save alfianlosari/cf2b8e420a2c81bb49def9dc58a5334a to your computer and use it in GitHub Desktop.
iOS TableView Data Provider
import UIKit
public class DataSourceProvider: NSObject, UITableViewDataSource, UITableViewDelegate {
private let dataManager: DataManager
init(dataManager: DataManager) {
self.dataManager = dataManager
super.init()
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataManager.itemsCount
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell
let item = dataManager.item(at: indexPath.row)
cell.config(with: item)
return cell
}
public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
dataManager.delete(at: indexPath.row)
tableView.reloadData()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment