我想创建一个进口UIKit的子类
class AlbumPlayerProgressBar:UICollectionView稍后在xib中使用它。并且似乎无法弄清楚如何正确使用init函数
import UIKit
class AlbumPlayerProgressBar: UICollectionView, UICollectionViewDataSource {
var progressBarType :ProgressBarType = .Player
var numOfSlides: Int = 0
var numOfPlayingSlide: Int = 0
init()
{
super.init()
self.register(UINib(nibName: NSStringFromClass(ProgressBarCell.self), bundle: nil), forCellWithReuseIdentifier: NSStringFromClass(ProgressBarCell.self))
}
func set(progressBarType :ProgressBarType, numOfSlides: Int, numOfPlayingSlide: Int) {
self.progressBarType = progressBarType
self.numOfSlides = numOfSlides
self.numOfPlayingSlide = numOfPlayingSlide
self.reloadData()
}
required init?(coder aDecoder: NSCoder) {
fatalError("AlbumPlayerProgressBar init(coder:) has not been implemented")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numOfSlides
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(ProgressBarCell.self), for: indexPath) as! ProgressBarCell
cell.setType(self.progressBarType)
if (indexPath.row < numOfPlayingSlide)
{
cell.setPlayed()
}
else if (indexPath.row == numOfPlayingSlide)
{
cell.setPlaying()
}
else
{
cell.setUnplayed()
}
return cell
}
}
enum ProgressBarType {
case Player
case Thumnail
}
答案 0 :(得分:0)
您可以将CollectionView声明为
class AlbumPlayerProgressBar: UICollectionView, UICollectionViewDataSource {
var progressBarType :ProgressBarType = .Player
var numOfSlides: Int = 0
var numOfPlayingSlide: Int = 0
override func awakeFromNib() {
super.awakeFromNib()
//gets called when you instantiate your collectionView from xib
self.register(UINib(nibName: NSStringFromClass(ProgressBarCell.self), bundle: nil), forCellWithReuseIdentifier: NSStringFromClass(ProgressBarCell.self))
self.dataSource = self
}
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
self.register(UINib(nibName: NSStringFromClass(ProgressBarCell.self), bundle: nil), forCellWithReuseIdentifier: NSStringFromClass(ProgressBarCell.self))
self.dataSource = self
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.register(UINib(nibName: NSStringFromClass(ProgressBarCell.self), bundle: nil), forCellWithReuseIdentifier: NSStringFromClass(ProgressBarCell.self))
self.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numOfSlides
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(ProgressBarCell.self), for: indexPath) as! ProgressBarCell
cell.setType(self.progressBarType)
if (indexPath.row < numOfPlayingSlide)
{
cell.setPlayed()
}
else if (indexPath.row == numOfPlayingSlide)
{
cell.setPlaying()
}
else
{
cell.setUnplayed()
}
return cell
}
}
取决于您如何初始化/实例化collectionView决定在哪里注册您的笔尖并将数据源设置为self。
希望有所帮助