Swift: ячейки CollectionView не отображаются до обновления представления - PullRequest
0 голосов
/ 13 июля 2020

Представление состоит из 4 кнопок навигации, которые переключают отображаемые представления коллекций. Я хочу, чтобы ячейки "друзья" отображались при первом запуске экрана

На данный момент. В представлении не будет отображаться какая-либо коллекция, пока не будет выбрана одна из кнопок навигации или страница не будет обновлена ​​с помощью swift refreshControl.

Вот два изображения, показывающие, как выглядит экран при первой загрузке и после того, как друзья нажата кнопка навигации

введите описание изображения здесь

обратите внимание, что это только часть файла, потому что он очень большой с множеством тривиальных функций для этой проблемы ... Если вы хотите увидеть .zip или другие функции, пожалуйста, позвольте я знаю.

    override func viewDidLoad() {
        super.viewDidLoad()
        backgroundView.layer.addBorder(edge: .bottom, color: .lightGray, thickness: 1)
        backgroundView.addViewGradient()
        setupViews()
        registerCells()
        loadBuckets()
        handledFriendsBtnTap()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        refresh()
    }
    
    private func refresh(){
        refreshControl.attributedTitle = NSAttributedString()
        refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
        collectionView.addSubview(refreshControl)
    }
    private func registerCells() {
        collectionView.register(FriendNotificationCell.self, forCellWithReuseIdentifier: "FriendCellId")
        collectionView.register(CommentNotificationCell.self, forCellWithReuseIdentifier: "CommentCellId")
        collectionView.register(LikeNotificationCell.self, forCellWithReuseIdentifier: "LikeCellId")
        collectionView.register(InspirationNotificationCell.self, forCellWithReuseIdentifier: "InspirationCellId")
    }

    @objc func refresh(_ sender: AnyObject) {
        self.collectionView.reloadData()
        refreshControl.endRefreshing()
    }
    
    @objc func handledFriendsBtnTap(){
        friendsBtn.addSubview(selectionDot)
        selectionDot.centerXAnchor.constraint(equalTo: friendsBtn.centerXAnchor).isActive = true
        selectionDot.bottomAnchor.constraint(equalTo: friendsBtn.bottomAnchor, constant: -6).isActive = true
        selectionDot.heightAnchor.constraint(equalToConstant: 5).isActive = true
        selectionDot.widthAnchor.constraint(equalToConstant: 5).isActive = true
        
        if friendArray.count == 0 {
            collectionView.addSubview(noContentLabel)
            noContentLabel.centerYAnchor.constraint(equalTo: collectionView.centerYAnchor, constant: -40).isActive = true
            noContentLabel.centerXAnchor.constraint(equalTo: collectionView.centerXAnchor).isActive = true
        } else {
            noContentLabel.removeFromSuperview()
        }
        
        pageIndex = 0
        screenLaunch = false
        self.collectionView.reloadData()
        
    }


extension NotificationsVC: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if pageIndex == 0{
            return setupFriendCell(indexPath: indexPath)
        }else if pageIndex == 1{
            return setupCommentCell(indexPath: indexPath)
        }else if pageIndex == 2{
            return setupLikeCell(indexPath: indexPath)
        }else if pageIndex == 3 {
            return setUpInspirationCell(indexPath: indexPath)
        }
        return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if pageIndex == 0 {
            return self.friendArray.count
        } else if pageIndex == 1 {
            return self.commentArray.count
        }else if pageIndex == 2 {
            return self.likeArray.count
        }else if pageIndex == 3 {
            return self.inspirationArray.count
        }
        return 0
    }
...