NSObject Class 中的 Collectionview 不起作用?
Collectionview in NSObject Class doesn't work?
我写了一个 class 包含一个 collectionview 显示在主 viewcontroller,
但它“从不”显示单元格数据。而且单元格背景颜色没有变黑..(变色只是为了测试,不是我的目的)..
(collectionView 可以正确显示)
我应该在哪里更正它?
class CellClass: NSObject,
UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout
{
let cellid = "cellid"
let cv : UICollectionView = {
let fl = UICollectionViewFlowLayout()
let v = UICollectionView(frame: .zero,
collectionViewLayout: fl)
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = UIColor.white
return v
}()
override init() {
super.init()
print("init")
cv.delegate = self
cv.dataSource = self
cv.register(Cell.self,
forCellWithReuseIdentifier: cellid)
if let window = UIApplication.shared.keyWindow {
window.addSubview(cv)
cv.frame = CGRect(x: 0,
y: window.frame.height - 300,
width: window.frame.width,
height: 300)
}
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
print("numberOfItemsInSection")
return 3
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("cell")
let vc = collectionView.dequeueReusableCell(withReuseIdentifier: cellid,
for: indexPath) as! Cell
vc.lbl.text = "test"
vc.backgroundColor = UIColor.black
return vc
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 300, height: 100)
}
}
class Cell: UICollectionViewCell {
let lbl : UILabel = {
let t = UILabel()
t.translatesAutoresizingMaskIntoConstraints = false
return t
} ()
override init(frame: CGRect) {
super.init(frame: frame)
lbl.frame = CGRect(x: 0,
y: 0,
width: 200,
height: 30)
addSubview(lbl)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我可能是错的,但我感觉你在 viewDidLoad
中做了类似的事情
let cellClass = CellClass()
一旦 didLoad 函数完成执行,cellClass
不再为了成为数据源和委托而持续存在。
您可以通过在 CellClass
中添加 deinit
来尝试
deinit {
print("Cell Class Deinit")
}
我相信它会被称为
我的建议是 persist
viewDidLoad
之外的对象
class AVPlayerScroll: UIViewController, UIGestureRecognizerDelegate
{
var cellClass: CellClass?
override func viewDidLoad()
{
super.viewDidLoad()
cellClass = CellClass()
}
我相信这应该会给您想要的结果
你可以在这里尝试一些事情。
- 您可以尝试以编程方式设置约束。像这样。
NSLayoutConstraint.activate([
lbl.leadingAnchor.constraint(equalTo: self.leadingAnchor),
lbl.topAnchor.constraint(equalTo: self.topAnchor),
lbl.widthAnchor.constraint(equalToConstant: 200),
actionSheetView.heightAnchor.constraint(equalToConstant: 30)])
- 您可以尝试在 layoutSubviews() 方法中将框架分配给 UILabel (lbl) 子视图。
我写了一个 class 包含一个 collectionview 显示在主 viewcontroller,
但它“从不”显示单元格数据。而且单元格背景颜色没有变黑..(变色只是为了测试,不是我的目的)..
(collectionView 可以正确显示)
我应该在哪里更正它?
class CellClass: NSObject,
UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout
{
let cellid = "cellid"
let cv : UICollectionView = {
let fl = UICollectionViewFlowLayout()
let v = UICollectionView(frame: .zero,
collectionViewLayout: fl)
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = UIColor.white
return v
}()
override init() {
super.init()
print("init")
cv.delegate = self
cv.dataSource = self
cv.register(Cell.self,
forCellWithReuseIdentifier: cellid)
if let window = UIApplication.shared.keyWindow {
window.addSubview(cv)
cv.frame = CGRect(x: 0,
y: window.frame.height - 300,
width: window.frame.width,
height: 300)
}
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
print("numberOfItemsInSection")
return 3
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("cell")
let vc = collectionView.dequeueReusableCell(withReuseIdentifier: cellid,
for: indexPath) as! Cell
vc.lbl.text = "test"
vc.backgroundColor = UIColor.black
return vc
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 300, height: 100)
}
}
class Cell: UICollectionViewCell {
let lbl : UILabel = {
let t = UILabel()
t.translatesAutoresizingMaskIntoConstraints = false
return t
} ()
override init(frame: CGRect) {
super.init(frame: frame)
lbl.frame = CGRect(x: 0,
y: 0,
width: 200,
height: 30)
addSubview(lbl)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我可能是错的,但我感觉你在 viewDidLoad
let cellClass = CellClass()
一旦 didLoad 函数完成执行,cellClass
不再为了成为数据源和委托而持续存在。
您可以通过在 CellClass
deinit
来尝试
deinit {
print("Cell Class Deinit")
}
我相信它会被称为
我的建议是 persist
viewDidLoad
class AVPlayerScroll: UIViewController, UIGestureRecognizerDelegate
{
var cellClass: CellClass?
override func viewDidLoad()
{
super.viewDidLoad()
cellClass = CellClass()
}
我相信这应该会给您想要的结果
你可以在这里尝试一些事情。
- 您可以尝试以编程方式设置约束。像这样。
NSLayoutConstraint.activate([ lbl.leadingAnchor.constraint(equalTo: self.leadingAnchor), lbl.topAnchor.constraint(equalTo: self.topAnchor), lbl.widthAnchor.constraint(equalToConstant: 200), actionSheetView.heightAnchor.constraint(equalToConstant: 30)])
- 您可以尝试在 layoutSubviews() 方法中将框架分配给 UILabel (lbl) 子视图。