Reputation: 95
I have a UITableViewController with three labels. The first two are from the .textLabel
and .detailTextLabel
, I have added a third label into the storyboard and hooked it up to a UITableViewCell
file. When I run, the app crashes unless the label is set as an optional with a "?", but nothing is still presented in the table. If it does not have the optional sign, it crashes and I get a response saying "unexpectedly found nil while unwrapping an optional value". The other two .textLabel
and .detailTextLabel
work fine. I would appreciate any help!
Here is my TableViewController
File,
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.groupscores.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(cellidentifier) as? ScoresPageCell
if (cell != nil) {
cell = ScoresPageCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellidentifier)
}
let groupscorelist: PFObject = self.groupscores.objectAtIndex(indexPath.row) as! PFObject
var scores: AnyObject = groupscorelist.objectForKey("Score")!
var user: AnyObject = groupscorelist.objectForKey("User")!
var info: AnyObject = groupscorelist.objectForKey("Info")!
cell!.textLabel?.text = "\(scores)"
cell?.detailTextLabel?.text = "\(user)"
cell!.UserNameCellLabel?.text = "\(info)"
return cell!
}
And my UITableViewCell File,
class ScoresPageCell: UITableViewCell {
@IBOutlet var UserNameCellLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
}
Upvotes: 1
Views: 57
Reputation: 3030
Follow this my friend:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(cellidentifier, forIndexPath: indexPath) as! ScoresPageCell
let groupscorelist = self.groupscores[indexPath.row]
var scores = groupscorelist["Score"] as! String // cast it to String if they are string.
var user = groupscorelist["User"] as! String
var info = groupscorelist["Info"] as! String
cell!.text1?.text = scores
cell?.text2?.text = user
cell!.UserNameCellLabel?.text = info
return cell
}
class ScoresPageCell: UITableViewCell {
@IBOutlet weak var text1: UILabel!
@IBOutlet weak var text2: UILabel!
@IBOutlet weak var UserNameCellLabel: UILabel!
}
Upvotes: 1