2017-09-03 36 views
2

我的应用程序需要几个CollectionViews,因此我决定以编程方式创建它们。我为每个单元添加了一个UILabel,但是UILabel仅添加到随机cell s,而其他单元不显示任何标签。以编程方式将UILabel添加到CollectionViewCells

如果我上下滚动文本标签随机在特定单元格内消失并重新出现在其他单元格上。 这里的问题是

的视觉描绘(注意标签消失而我向下滚动)

我需要所有单元,以显示textLabels。

这是我使用的代码:

视图控制器

override func viewDidLoad() { 
    super.viewDidLoad() 


    let frame =     CGRect(x: 0 , y: 50 , width: 375 , height: 300) 
    let layout =     UICollectionViewFlowLayout() 
    let collectionView =   UICollectionView(frame: frame , collectionViewLayout: layout) 
    collectionView.delegate = self 
    collectionView.dataSource = self 
    collectionView.register(CustomCell.self, forCellWithReuseIdentifier: NSStringFromClass(CustomCell.self)) 
    view.addSubview(collectionView) 
} 



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 250 } 

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

    let cell =    collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(CustomCell.self), for: indexPath) as! CustomCell 
    cell.nameLabel.text = "XYZ" 
    return cell 

} 

II自定单元

class CustomCell: UICollectionViewCell { 

var nameLabel: UILabel! 

override init(frame : CGRect) { 
    super.init(frame : frame) 

    nameLabel =     UILabel(frame: self.frame) 
    nameLabel.textAlignment = .left 
    nameLabel.textColor =  .black 
    contentView.addSubview(nameLabel) 
    contentView.backgroundColor = .red 
    } 

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

    } 

是什么原因造成ü异常行为?我是否错误地设置了我的collectionView

+0

func CollectionView(_ CollectionView:UICollectionView,heightForRowAt indexPath:IndexPath) - > CGFloat {。这是什么?这是如何返回UICollectionViewCell的大小https://developer.apple.com/documentation/uikit/uicollectionviewdelegateflowlayout/1617708-collectionview – 2017-09-03 12:13:21

+0

我很抱歉,这是一个错字。我更新了代码。 – 6994

+0

以编程方式创建UILabel需要您设置IB为您所做的所有属性,因此请参阅:https://stackoverflow.com/questions/17813121/programmatically-creating-uilabel另外,您是否尝试过UILabel到前面? 第二个问题,看起来像你的出队问题,尝试设置awakeFromNib方法内的标签。 – OhadM

回答

2

您在这一行中做错了nameLabel = UILabel(frame: self.frame),实际上您给出的位置与收集视图单元格的位置(x和y坐标)相同,这是错误的。你需要给nameLabel的位置x = 0和y = 0,那么它会正常工作。定义nameLabel的帧如下:

var nameLabel: UILabel = UILabel(frame: CGRect.zero) 

override init(frame : CGRect) { 
    super.init(frame : frame) 
    nameLabel.frame.size = CGSize(width: self.frame.width, height: self.frame.height) 
} 

输出如下:

enter image description here

+0

感谢您的解决方案。这是一个非常粗心的错误。 – 6994

1

它看起来像一个出队问题,因为它发生在单元出队期间。

你应该初始化的UILabel的awakeFromNib方法中 - 每细胞被离队,您将创建你所需要的标签时这样

此外,由于您是以编程方式创建UILabel,因此您应该查看此answer,基本上,当您以编程方式创建UILabel时,应该设置更多的值。

我也建议在创建标签时使用bringSubViewToFront

+0

感谢您的回答。我不知道'awakeFromNib'会如何使用,因为我不使用'InterfaceBuilder'。另外我尝试在'cellForItemAtIndexPath'方法中使用'bringSubviewToFront',但那也没有效果。 – 6994

+0

只需重写awakeFromNib并将代码重构为awakeFromNib - 对故事板和笔尖都有好处。如果你仍然坚持,你可以在单元格内创建一个设置方法(重构你的UILabel代码)并在cellForRow中出列后调用它,但这是一个糟糕的做法 – OhadM

+0

我以编程方式做所有事情,因此既没有故事板也没有可擦写。 – 6994

0

的问题是,每个小区具有不同值frame.origin因此设定UILabel(frame : self.frame)将是不正确。

例如:

let label = UILabel(frame: self.frame) 
    print(self.frame)      // (130.0, 420.0, 50.0, 50.0) 

// the origin of the label is now CGPoint(130.0, 420.0) with respect to the 
//cell it would be totally out of bounds somewhere down below.    

的每个UILabel需要的originCGPoint(x :0,y :0),使得其正好出现在其各自小区的顶部。

因此,简单的解决方案是用边界替换帧。

let label = UILabel(frame: self.bounds) 
相关问题