从单元格中的按钮重新加载uicollection视图控制器

时间:2018-04-02 13:22:22

标签: ios swift

我在用户好友请求单元格中有一个按钮,当我拒绝它从数据库中删除他们的待处理好友请求时,它会从数据库中删除信息就好但我无法访问UICollectionViewController cell属于,要从数据库中重新获取待处理的请求?因此,它将从数据库中获取朋友的请求,而我刚刚拒绝的那个请求将不在那里,此刻我必须离开屏幕并回来让它消失?

是否无法通过UICollectionView内的单元格按钮调用UICollectionView的功能?

protocol friendsRequestControllerDelegate {

    func fetch()
    }

    class FriendsRequestsCell: UICollectionViewCell, CLLocationManagerDelegate  {

    var delegate: friendsRequestControllerDelegate?

    lazy var declineButton: UIButton = {
        let b = UIButton(type: .system)
        b.backgroundColor = UIColor.black
        b.setTitle("Decline", for: .normal)
        b.setTitleColor(UIColor.white, for: .normal)
        b.addTarget(self, action: #selector(declineFriendRequest), for: .touchUpInside)
        b.layer.cornerRadius = 3
        b.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)

        return b
    }()

    func declineFriendRequest(){
        print("not yet set")
        guard let userAtRow = user?.uid else { return }

        guard let uid = Auth.auth().currentUser?.uid else { return }            

        let db = Firestore.firestore()

        db.collection("Users").document(uid).collection("Pending Requests").document(userAtRow).delete() { err in
            if let err = err {
                print("Error removing document: \(err)")
            } else {
                print("Document successfully removed!")
                self.frc.collectionView?.reloadData()
            }
        }

      delegate?.fetch()

      }    


    class FriendsRequestsController: UICollectionViewController, UICollectionViewDelegateFlowLayout,friendsRequestControllerDelegate {

    func fetch(){
        firestoreFetchFriendsRequestsUserIds()
        viewWillAppear(true)
    }

    //collectionView?.reloadData()

     func firestoreFetchFriendsRequestsUserIds() {

        guard let uid = Auth.auth().currentUser?.uid else { return }

        let db = Firestore.firestore()

        db.collection("Users").document(uid).collection("Pending Requests").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {   

                    let d = document.data()

                    d.forEach({ (key: String, value: Any) in

                        print("this is the key",key)

                        Database.firestorefetchUserWithUID(uid: key, completion: { (user) in

                            self.users.append(user)

                            self.collectionView?.reloadData()
                        }    
                        )}   
                    )}   
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您的集合单元格中有一些控制元素(让它成为一个按钮)。当你激活它时(例如按下按钮),一些进程开始,当它完成时,必须更新集合视图。

如果那是您面临的情况(即我理解正确的话),我会采取以下方式:

  1. 为您的细胞制定协议:

    class CustomCollectionViewCell: CollectionViewCell {
    
        // Some your stuff
    
        weak var delegate: CustomCollectionViewCellDelegate?
    
        @IBAction func buttonPressed(_ sender: UIButton) {
            yourAsyncProcess() {
                self.delegate?.reloadData()
            }
        }
    }
    
  2. 修改自定义单元格类以使用协议

    func reloadData() {
        DispatchQueue.main.async {
            self.collectionView.reloadData()
        }
    }
    
  3. 您的集合视图控制器采用该协议并添加以下方法:

    delegate
  4. 更新单元格时设置单元格override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell: CustomCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCustomCollectionViewCellReuseIdentifier", for: indexPath) as! CustomCollectionViewCell // Your custom cell setup setup cell.delegate = self return cell } 属性

    org.apache.spark.ml.linalg.Vector
相关问题