Home > Mobile >  Collectionview in NSObject Class doesn't work?
Collectionview in NSObject Class doesn't work?

Time:02-25

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

CodePudding user response:

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

CodePudding user response:

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.
  • Related