NSObject 类中的 Collectionview 不起作用?

发布于 2025-01-10 08:32:39 字数 2660 浏览 0 评论 0原文

我编写了一个包含集合视图的类以在主视图控制器中显示, 但它“从不”显示细胞数据。并且单元格背景颜色没有更改为黑色..(更改颜色仅用于测试,不是我的目的)..
(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")
    }
}

I wrote a class contain a collectionview to show in main viewcontroller,
but it was "never" shows the cell data. And the cell background color wasn't change to black..(change color is only for test, not my purpose)..
(The collectionView can be showed correctly)
Where I should to correct it?

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")
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晨说晚安 2025-01-17 08:32:39

我可能是错的,但我有一种感觉,您在 viewDidLoad 中做了类似的事情

let cellClass = CellClass()

,一旦 didLoad 函数完成执行,cellClass 就不再持久存在以成为数据源和委托。

您可以通过将 deinit 添加到您的 CellClass 来尝试此操作

deinit {
    print("Cell Class Deinit")
}

,我相信它将被称为

我的建议是 persist 超出 的对象code>viewDidLoad

class AVPlayerScroll: UIViewController, UIGestureRecognizerDelegate
{
    var cellClass: CellClass?

    override func viewDidLoad()
    {
        super.viewDidLoad()
        cellClass = CellClass()
    }

我相信这应该会给你你正在寻找的结果

I could be wrong but I have a feeling you do something like this in your viewDidLoad

let cellClass = CellClass()

As soon as the didLoad function completes its execution, cellClass no longer persists in order to be the datasource and delegate.

You can try this by adding a deinit to your CellClass

deinit {
    print("Cell Class Deinit")
}

And I believe it will be called

What I suggest instead is to persist the object beyond viewDidLoad

class AVPlayerScroll: UIViewController, UIGestureRecognizerDelegate
{
    var cellClass: CellClass?

    override func viewDidLoad()
    {
        super.viewDidLoad()
        cellClass = CellClass()
    }

I believe this should give you the results you are looking for

梦里南柯 2025-01-17 08:32:39

你可以在这里尝试一些事情。

  1. 您可以尝试以编程方式设置约束。像这样的东西。

NSLayoutConstraint.activate([
lbl.leadingAnchor.constraint(equalTo: self.leadingAnchor),
lbl.topAnchor.constraint(equalTo: self.topAnchor),
lbl.widthAnchor.constraint(equalToConstant: 200),
actionSheetView.heightAnchor.constraint(equalToConstant: 30)])

  1. 您可以尝试在 layoutSubviews() 方法中将框架分配给 UILabel (lbl) 子视图。

you can try couple of things here.

  1. You can either try to set the constraints programmatically. Something like this.

NSLayoutConstraint.activate([
lbl.leadingAnchor.constraint(equalTo: self.leadingAnchor),
lbl.topAnchor.constraint(equalTo: self.topAnchor),
lbl.widthAnchor.constraint(equalToConstant: 200),
actionSheetView.heightAnchor.constraint(equalToConstant: 30)])

  1. You can try to assign the frame to the UILabel (lbl) subview in layoutSubviews() method.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文