Reputation: 625
I am trying to implement the Tableview protocol in a custom class of my own. My purpose is to develop a TableView library.
my sample class looks like this
open class TempTableAdapter : NSObject, UITableViewDataSource, UITableViewDelegate{
open var listTable : UITableView!
init(listTable:UITableView) {
super.init()
self.listTable = listTable
self.listTable.dataSource = self
self.listTable.delegate = self
}
open func numberOfSections(in tableView: UITableView) -> Int {
return 1 ;
}
open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "temp") as! TempCell
let text = "hello"
cell.label.text = text
return cell
}
open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print ("tapped")
}
}
and my ui controller looks like this
class ViewController: UIViewController {
@IBOutlet weak var listTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let adap : TempTableAdapter = TempTableAdapter (listTable: listTable)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Can anyone point out what am I missing?
The problem is the UITableView
is showing the default table, not using my custom class.
Upvotes: 0
Views: 84
Reputation: 1380
The adapter gets deallocated once the viewDidLoad
methods exits, because it is a local variable. Instead, make it a property of the view controller:
let adapter: TempTableAdapter!
override func viewDidLoad() {
super.viewDidLoad()
adapter = TempTableAdapter (listTable: listTable)
}
Hope this helps! Good luck!
Upvotes: 3