gpt4 book ai didi

ios - 以编程方式创建 UICollectionView(在 UIViewController 之外)

转载 作者:行者123 更新时间:2023-11-30 11:25:14 25 4
gpt4 key购买 nike

我想创建一个 import UIKit 的子类

类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
}

最佳答案

您可以将 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来决定在哪里注册您的nib并将数据源设置为self。

希望对你有帮助

关于ios - 以编程方式创建 UICollectionView(在 UIViewController 之外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50812371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com