スクロールすると、ボタンのラベルがスタック/再生成されます。したがって、最初のラベルは、ビューを離れて再入力した後、その上にElabelが付いたAlabelを表示する場合があります。私は単にスクロール可能なボタン(またはそのことについてはラベル)の単一の行を作成しようとしています。コレクションビューとセルは、ストーリーボードを介して作成されました。コードはCVに正しい数のセルを生成しますが、スクロールするとラベルが階層化されます(水平)。
let buttonLabels = ["Alabel", "Blabel", "Clabel", "Dlabel", "Elabel", "Flabel", "Glabel", "Hlabel", "Ilabel", "Jlabel", "Klabel", "Llabel", "Mlabel"]
@IBOutlet weak var btnCollVw: UICollectionView!
//loadColFlowLayout() is called from ViewDidLoad()
func loadColFlowLayout() {
let btnLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
btnLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
btnLayout.sectionInset = UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 1)
btnLayout.itemSize = CGSize(width: 63, height: 30)
btnCollVw.collectionViewLayout = btnLayout
btnCollVw!.backgroundColor = UIColor.whiteColor()
}
func numberOfSections() -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return buttonLabels.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell: UICollectionViewCell = self.iconCollVw.dequeueReusableCellWithReuseIdentifier("swIconsCell", forIndexPath: indexPath) as UICollectionViewCell
var makeButton = UIButton(frame: CGRectMake(0, 0, 63, 29))
makeButton.setTitle(buttonLabels[indexPath.item], forState: .Normal)
makeButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
cell.addSubview(makeButton)
// or using cell.contentView.addSubview(makeButton)
return cell
}
}
セルの再利用の問題。コレクションビューからセルをデキューするたびに、すでにボタンが表示されている可能性があります。セルに既知のタグが付いているボタンを確認するときに、少し改善されたバージョンを確認してください。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("swIconsCell", forIndexPath: indexPath) as UICollectionViewCell
if cell.viewWithTag(1234) == nil {
var makeButton = UIButton(frame: CGRectMake(0, 0, 63, 29))
makeButton.setTitle(buttonLabels[indexPath.item], forState: .Normal)
makeButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
makeButton.tag = 1234;
cell.addSubview(makeButton)
// or using cell.contentView.addSubview(makeButton)
}
return cell
}
別の方法で同じことを行うことができます。たとえば、次のサブクラスを作成します。 UICollectionViewCell
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加