Created
May 21, 2018 15:15
-
-
Save damodarnamala/c6baec148cc65fbc6bddee11d52792a6 to your computer and use it in GitHub Desktop.
UITableview Reusable Delegate Datasource
This file contains 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 | |
class GenericTableSource : NSObject { | |
private var cellID: String = "" | |
private (set) var data : [Any] = [] | |
func registerCell(identifier: String, data : [String], tableView: UITableView) { | |
if identifier.isEmpty { | |
fatalError("Tableview cell identifier can't be empty or nil") | |
} else { | |
self.cellID = identifier | |
self.data = data | |
tableView.register(UITableViewCell.self, forCellReuseIdentifier: self.cellID) | |
} | |
} | |
func cell(indexPath : IndexPath, tableview : UITableView)-> UITableViewCell { | |
let cell = tableview.dequeueReusableCell(withIdentifier: self.cellID) | |
cell?.textLabel?.text = self.data[indexPath.row] as? String | |
return cell! | |
} | |
} | |
//MARK: Tableview Delegate and DataSource | |
extension GenericTableSource : UITableViewDelegate, UITableViewDataSource { | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return self.data.count | |
} | |
func numberOfSections(in tableView: UITableView) -> Int { | |
return 1 | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
return self.cell(indexPath: indexPath, tableview: tableView) | |
} | |
} |
This file contains 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 | |
class ViewController: UIViewController { | |
@IBOutlet weak var tableListView : UITableView? | |
@IBOutlet weak var tableListFruits : UITableView? | |
var genericListTableSource = GenericTableSource() | |
var genericFruitsTableSource = GenericTableSource() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// list tabeview | |
let listItems = ["Once","Two","Three","Four"] | |
genericListTableSource.registerCell(identifier: "cellList", data: listItems, tableView: self.tableListView!) | |
self.tableListView?.dataSource = genericListTableSource | |
self.tableListView?.delegate = genericListTableSource | |
// fruits tableView | |
let fruitItems = ["Apple","Mango","Banana"] | |
genericFruitsTableSource.registerCell(identifier: "cellList", data: fruitItems, tableView: self.tableListFruits!) | |
self.tableListFruits?.dataSource = genericFruitsTableSource | |
self.tableListFruits?.delegate = genericFruitsTableSource | |
DispatchQueue.main.async { | |
self.tableListFruits?.reloadData() | |
self.tableListView?.reloadData() | |
} | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good job.
Please submit your solutions using these links
This will help me to better organise the data, and I will respond you properly in thread by review your code.