Skip to content

Instantly share code, notes, and snippets.

@damodarnamala
Created May 21, 2018 15:15
Show Gist options
  • Save damodarnamala/c6baec148cc65fbc6bddee11d52792a6 to your computer and use it in GitHub Desktop.
Save damodarnamala/c6baec148cc65fbc6bddee11d52792a6 to your computer and use it in GitHub Desktop.
UITableview Reusable Delegate Datasource
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)
}
}
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.
}
}
@hitendradeveloper
Copy link

Good job.
Please submit your solutions using these links

  1. Just fill this Google form: https://goo.gl/forms/zj2YrQu3H1LTbEKw2
  2. Solution submission: https://goo.gl/forms/RIgCO1EAqmHgPGPN2

This will help me to better organise the data, and I will respond you properly in thread by review your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment