When I load my app this is what happens. For some reason the table view loads blank. And only when I select the cell and click another cell does the previously selected cell show up. But for the first cell, no matter where/how I click I still can't see the first cell.
I don't understand why this happening b/c I followed this tutorial. Obviously I'm doing something wrong and would be very grateful if someone could tell me why this is happening & how to fix it.
In the main storyboard I'm using a tableViewController SummaryTableViewController
and a custom UITableViewCell DayOfWeekTableViewCell
. I have a nib file for the table view cell called DayofWeekSpendingTableViewCell.xib
. Here's a list of all my files and their fileneames.
This is my SummaryTableViewController
code:
class SummaryTableViewController: UITableViewController {
var dayOfWeek: [String] = [String]()
var totalSpentPerDay: [Double] = [Double]()
override func viewDidLoad() {
super.viewDidLoad()
dayOfWeek = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]
totalSpentPerDay = [0, 7.27, 0, 0, 39, 0, 0]
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dayOfWeek.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("summaryCell", forIndexPath: indexPath) as! DayOfWeekTableViewCell
// Configure the cell...
let nib = NSBundle.mainBundle().loadNibNamed("DayofWeekSpendingTableViewCell", owner: self, options: nil)
cell = nib[0] as! DayOfWeekTableViewCell
cell.dayOfWeek.text = dayOfWeek[indexPath.row]
cell.totalAmountSpent.text = String(totalSpentPerDay[indexPath.row])
return cell
}
}
This is my custom cell DayOfWeekTableViewCell
code:
class DayOfWeekTableViewCell: UITableViewCell {
@IBOutlet weak var dayOfWeek: UILabel!
@IBOutlet weak var totalAmountSpent: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
In the main storyboard, I've...
DayOfWeekTableViewCell
to the storyboard cellsummaryCell
In the nib file DayofWeekSpendingTableViewCell.xib
I've...
Instead of loading the NIB in cellForRowAtIndexPath, load the NIB in viewDidLoad() and register it for use with your table view
override func viewDidLoad() {
super.viewDidLoad()
dayOfWeek = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]
totalSpentPerDay = [0, 7.27, 0, 0, 39, 0, 0]
// Create a nib for reusing
let nib = UINib(nibName: "DayofWeekSpendingTableViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "summaryCell")
}
Then you could directly access the cell in cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Configure the cell...
let cell = tableView.dequeueReusableCellWithIdentifier("summaryCell", forIndexPath: indexPath) as! DayOfWeekTableViewCell
cell.dayOfWeek.text = dayOfWeek[indexPath.row]
cell.totalAmountSpent.text = String(totalSpentPerDay[indexPath.row])
return cell
}