【问题标题】:Subclass UICollectionView子类 UICollectionView
【发布时间】:2014-12-12 14:00:48
【问题描述】:

所以,我需要扩展UICollectionView 功能,覆盖单元格等。 在每个示例中,我都看到了 UICollectionViewController 的子类化,但不仅仅是 UICollectionView

我不想将我的新类(称为GridSet)作为情节提要中单个页面(UIViewController)的超类,因为我有更多元素(自定义按钮、标签等)而且我没有想在我的新课程中维护它们,但我想在我的页面UIViewController 中维护它们。

我什至想在一个UIViewController 中插入我的两个子类并在情节提要中执行(但你不能将一个UIViewController 放在另一个UIViewController 中。

问题是,当我对UICollectionViewUICollectionViewDataSource 进行子类化时,它似乎甚至没有初始化。

class GridSet: UICollectionView, UICollectionViewDataSource {

let computers = [
    ["Name" : "MacBook Air", "Color" : "Silver"],
    ["Name" : "MacBook Pro", "Color" : "Silver"],
    ["Name" : "iMac", "Color" : "Silver"],
    ["Name" : "Mac Mini", "Color" : "Silver"],
    ["Name" : "Mac Pro", "Color" : "Black"]
]

override func awakeFromNib() {
    super.awakeFromNib()

    let nib = UINib(nibName: "GridSetCell", bundle: nil)
    self.registerNib(nib, forCellWithReuseIdentifier: "GridSetCellDefault")
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return computers.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = self.dequeueReusableCellWithReuseIdentifier("GridSetCellDefault", forIndexPath: indexPath) as GridSetCell

    cell.label = computers[indexPath.row]["Name"]!

    return cell
}

}

我将笔尖用于自定义单元格:

class GridSetCell: UICollectionViewCell {

@IBOutlet private weak var cLabel: UILabel!
@IBOutlet private weak var cBack: UIImageView!

var label: String = "" {
    didSet {
        if (label != oldValue) {
            cLabel.text = label
        }
    }
}

var back: String = "" {
    didSet {
        if (back != oldValue) {
            cBack.image = UIImage(named: back)
        }
    }
}

}

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

    首先将UICollectionViewCellUICollectionViewLayout 子类化以实现您的目标,在系统UICollectionView 中使用您的版本。本教程显示了以下步骤: http://skeuo.com/uicollectionview-custom-layout-tutorial 当这些工作正常时,如果您仍然需要子类化 UICollectionView,您将拥有工作组件来“安装”到新的子类中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 2014-08-18
      • 1970-01-01
      相关资源
      最近更新 更多