Created
August 5, 2018 15:06
-
-
Save alfianlosari/cf2b8e420a2c81bb49def9dc58a5334a to your computer and use it in GitHub Desktop.
iOS TableView Data Provider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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