2017-10-12 53 views
2

我试图按照this 但是,当我做cell.checkmarkView.checked = true它返回零,所以一切都崩溃。 这里是我的MyCollectionViewCell:UICollectionViewCell checkmark return nil

class MyCollectionViewCell: UICollectionViewCell { 

@IBOutlet weak var myLabel: UILabel! 

var checkmarkView: SSCheckMark! 

override init(frame: CGRect) { 
    super.init(frame: frame) 
    checkmarkView = SSCheckMark(frame: CGRect(x: frame.width-40, y: 10, width: 35, height: 35)) 
    checkmarkView.backgroundColor = UIColor.white 
    contentView.addSubview(checkmarkView) 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    //fatalError("init(coder:) has not been implemented") 
}} 

你能帮助我了解什么是错的?

我的CollectionView cellforitemat ...

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell 
    cell.myLabel.text = self.listCivics()[indexPath.item] 
    cell.layer.borderWidth = 1 
    cell.checkmarkView.checked = true 
    return cell 
} 
+0

“cell.checkmarkView.checked = true它返回nil”什么返回nil?你是否输入了'override init(frame:CGRect){'? – Larme

+0

如果你是在谈论MyCollectionViewCell是的,我做你可以看到 – John

+0

顺便说一句:最好是使用内容视图的“边界”而不是“框架”,你应该设置'autoresizingMask'。我会适当地更新我的答案。 – clemens

回答

0

你的细胞是从故事板加载。因此,init(frame:)未被调用。您还应该初始化init(coder:)中的复选标记。

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    let contentBounds = contentView.bounds 
    checkmarkView = SSCheckMark(frame: CGRect(x: contentBounds.width - 40, y: 10, width: 35, height: 35)) 
    checkmarkView.backgroundColor = UIColor.white 
    // adapt to changes in cell size 
    checkmarkView.autoresizingMask = [ .flexibleLeftMargin, .flexibleBottomMargin ] 
    contentView.addSubview(checkmarkView) 
} 
相关问题