未显示UITableViewCell中的UILabel

时间:2015-08-27 01:57:15

标签: ios swift

我有一个带有三个标签的UITableViewController。前两个来自.textLabel.detailTextLabel,我在故事板中添加了第三个标签,并将其连接到UITableViewCell文件。当我运行时,除非将标签设置为带有“?”的可选项,否则应用程序会崩溃,但表中仍未显示任何内容。如果它没有可选的标志,它会崩溃并且我得到一个回复​​,说明“在解开可选值时意外发现了nil”。另外两个.textLabel.detailTextLabel工作正常。我将不胜感激任何帮助!

这是我的TableViewController文件,

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!

}

我的UITableViewCell文件,

class ScoresPageCell: UITableViewCell {

    @IBOutlet var UserNameCellLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

}

1 个答案:

答案 0 :(得分:1)

关注我的朋友:

 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!

}