ios - 未显示 UITableViewCell 中的 UILabel

标签 ios swift

我有一个带有三个标签的 UITableViewController。前两个来自 .textLabel.detailTextLabel,我在 Storyboard中添加了第三个标签并将其连接到 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()
    }

}

最佳答案

关注我的 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!

}

关于ios - 未显示 UITableViewCell 中的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32239464/

相关文章:

swift - 如何在NSDatePicker中正确使用rangeDataMode?

swift - 从 Swift 开源版本访问 vscanf 等

ios - WKWebKit takeSnapshot(带有快照配置:) not working on iOS device

ios - UITableview 部分索引点击事件

ios - 链接错误 Xcode 7.2 : Framework Not Found

ios - Mac OS X Lion/Mountain Lion 缓存未命中分析

ios - UITableView自定义删除按钮功能

iphone - 如何在 xcode 中创建模板子类文件?

ios - 创建 UIAlertController 而不呈现它会产生警告

swift - 在 iOS userInfo 上使用 Swift 的 Decodable