Hi i want to populate this tableview
i don't use the static table view because i take an error, and i decide to use the dynamic table view and disable the scroll in the table. The table view is use only for the information :
@IBOutlet var tableInfoQuake: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = quake.place
tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.textLabel?.text = quake.place
tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.textLabel?.text = "Mag: \(quake.mag)"
tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.textLabel?.text = "Cordinate: (\(quake.longitude),\(quake.latitude))"
tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.textLabel?.text = "Depth: \(quake.depth)"
but in the table view i don't visualize nothing. how can i do? thank you.
EDIT: i do this to modify each row :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cellIdentifier = "quakeInfo"
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell
if indexPath == NSIndexPath(forRow: 0, inSection: 0){
cell.textLabel!.text = quake.place
cell.detailTextLabel!.text = "Mag: \(quake.mag)"
}
if indexPath == NSIndexPath(forRow: 1, inSection: 0){
cell.textLabel!.text = "Cordinate: (\(quake.longitude),\(quake.latitude))"
cell.detailTextLabel!.text = "Depth: \(quake.depth)"
}
return cell
}
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With