Reputation: 47
My code:
import Foundation
import Firebase
class CellOneViewController: UITableViewCell {
@IBOutlet weak var new1: UILabel!
let ref = Firebase(url: "https://burning-heat-8250.firebaseio.com/slide2")
func viewdidload() {
ref.observeEventType (.Value, withBlock: { snapshot in
self.new1.text = snapshot.value as? String
})
}
}
I've read around that you can't call viewDidLoad in a UITableViewCell, only in a UITableViewController. All the answers are in Objective-C, but I'm writing the app in Swift. I don't receive any critical errors but when running the app nothing appears in the cell where the label is. I'm fairly new to using Xcode, as I am just going around following guides so if I'm saying something incorrect let me know.
Upvotes: 4
Views: 6446
Reputation: 2598
I think, you need method func layoutSubviews().
Only ViewController gets func viewDidLoad() called, after view is loaded.
If you need to initialize something or update views, you need to do in layoutSubviews(). As soon, your view or UITableViewCell gets loaded, layoutSubviews() get called.
Replace viewDidLoad with layoutSubviews()
class CellOneViewController: UITableViewCell {
@IBOutlet weak var new1: UILabel!
let ref = Firebase(url: "https://burning-heat-8250.firebaseio.com/slide2")
override func layoutSubviews() {
super.layoutSubviews()
ref.observeEventType (.Value, withBlock: { snapshot in
self.new1.text = snapshot.value as? String
})
}
}
Upvotes: 3
Reputation: 1195
The reason you can't do this is that a UITableViewCell
isn't a subclass of UIViewController.
The cellForRowAtIndexPath
method in the UITableViewDataSource is where you should set up your cells. You probably only want to do something like this in your custom UITableViewCell
:
class CellOne: UITableViewCell {
@IBOutlet weak var new1: UILabel!
}
Then in your TableViewController's cellForRowAtIndexPath
method (provided you have imported firebase) you should dequeue a reusable cell, cast it as a CellOne (as! cellOne
) and then you can set the new1.text
value. I don't know what your reuse identifier is, but it would look something like this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Your-Reuse-Identifier", forIndexPath: indexPath) as! CellOne
cell.new1.text = "Your Value"
return cell
}
Upvotes: 1