nill分配数据标签时(在展开Optional值时意外发现nil)

时间:2015-09-29 14:29:06

标签: swift

代码的第一部分The string1 outlet from the OptionalListViewController to the UILabel is invalid. Outlets can not be connected to repeating content

import UIKit

class ListsTableViewCell: UITableViewCell {

    override func awakeFromNib() {
       super.awakeFromNib()
       // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
       // Configure the view for the selected state
    }

    @IBOutlet weak var string1: UILabel!

    @IBOutlet weak var string2: UILabel!

    @IBOutlet weak var string3: UILabel!

    @IBOutlet weak var string4: UILabel!

    @IBOutlet weak var string5: UILabel!

}
var cellyItem = ListsTableViewCell()

override func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath:
    NSIndexPath) -> ListsTableViewCell {


        let cell:ListsTableViewCell = tableView.dequeueReusableCellWithIdentifier("Celly", forIndexPath: indexPath) as! ListsTableViewCell


        let dic:NSDictionary = _items.objectAtIndex(indexPath.row) as! NSDictionary
        print(" Plist:\(dic)")

        let str0:NSString = dic["0"]! as! NSString
        let str1:NSString = dic["1"]! as! NSString
        let str2:NSString = dic["2"]! as! NSString
        let str3:NSString = dic["3"]! as! NSString
        let str4:NSString = dic["4"]! as! NSString
        let str5:NSString = dic["5"]! as! NSString
        let str6:NSString = dic["6"]! as! NSString
        let str7:NSString = dic["7"]! as! NSString
        let str8:NSString = dic["8"]! as! NSString

        cellyItem.string1.text = (str0 as String) + (str1 as String)
        cellyItem.string2.text = (str2 as String) + (str3 as String)
        cellyItem.string3.text = (str4 as String) + (str5 as String)
        cellyItem.string4.text = (str6 as String) + (str7 as String)
        cellyItem.string5.text = (str4 as String) + (str8 as String)

        return cell
}

cellyItem.string1,2,3,4,5 - 收到nill

为什么会这样?好吧!这个“str0,1,2,3,4,5,6,7,8”接收数据。也许我需要初始化子类?

2 个答案:

答案 0 :(得分:1)

很抱歉我还不能发表评论,但我注意到的很奇怪:为什么你要使用cellyItem?你应该在任何地方使用你的dequed单元格。像:

cell.string1.text = str0 + str1等...

答案 1 :(得分:1)

非常感谢您的帮助 - 答案非常简单。 Nill获得因为它没有创建tableview。解决方案是创建一条线 " tableView.registerNib(UINib(nibName:" ListsTableViewCell",bundle:nil),forCellReuseIdentifier:" Celly")"