Hi i want to populate this tableview
i don\'t use the static table view because i tak
So, you want to dynamically populate a tableview. Well... here you go my friend:
Okay.... so you know what a protocol is? Well if you don't ... BAM, now you do (Not me in video). A DataSource is what the UITableView will call to retrieve the data it requires to display. To be specific, the DataSource in question is UITableViewDataSource.
so...
// change top to...
class MyViewController:UIViewController, UITableViewDataSource
This makes UITableViewDataSource a requirement for your ViewController. Next you need to set the datasource on your UITableView. So....
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = quake.place
tableInfoQuake.datasource = self
}
Lets say you want to now add 7 cells to the table, and all 7 will say "Hello Man". For this example, I am going to do it the worst yet simplest way. if you want me to go more in depth, just comment below, and I will do it the better way.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let label = UILabel(CGRect(x:0, y:0, width:200, height:50))
label.text = "Hello Man"
cell.addSubview(label)
return cell
}
So thats it for step one. We have told the table that the ViewController has what it is looking for in terms of data, and we have returned data for it to use.
Much like Step 1, this step starts with adding something to the top...
// change top to...
class MyViewController:UIViewController, UITableViewDataSource, UITableViewDelegate
Then, again, much like the top, we will edit the viewDidLoad() function...
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = quake.place
tableInfoQuake.datasource = self
tableInfoQuake.delegate = self
}
And now we must implement the delegate methods that designate the height of each cell.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
We have now added the delegate to the table, and implemented the method that tells the UITableView what the height of each cell is.
class MyViewController:UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableInfoQuake: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableInfoQuake.datasource = self
tableInfoQuake.delegate = self
}
// UITableViewDataSource Functions
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let label = UILabel(CGRect(x:0, y:0, width:200, height:50))
label.text = "Hello Man"
cell.addSubview(label)
return cell
}
// UITableViewDelegate Functions
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
}
Good luck to you.
ZR
The cells in the storyboard are prototype cells. That means they are not actually in the table view, but can be used as templates.
In order to populate the tableview, you need to: