ios - 从单元格中的按钮重新加载 Collection View Controller

标签 ios swift

我在用户好友请求单元格中有一个按钮,当我拒绝它时,它会从数据库中删除他们待处理的好友请求,它会很好地从数据库中删除信息,但我无法访问 UICollectionViewController 该单元格所属,从数据库中重新获取待处理的请求?因此它会从数据库中获取 friend 的请求,而我刚刚拒绝的请求不会在那里,现在我必须离开屏幕再回来才能让它消失吗?

是否无法从 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()
                        }    
                        )}   
                    )}   
            }
        }
    }

最佳答案

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

如果这是您面临的情况(即我对您的理解是正确的),我会执行以下操作:

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

    /// Used by cells to pass commands to their collection view
    protocol CustomCollectionViewCellDelegate: class {
        /// Requests the delegate to update data
        func reloadData()
    }
    
  2. 修改您的自定义单元类以使用该协议(protocol)

    class CustomCollectionViewCell: CollectionViewCell {
    
        // Some your stuff
    
        weak var delegate: CustomCollectionViewCellDelegate?
    
        @IBAction func buttonPressed(_ sender: UIButton) {
            yourAsyncProcess() {
                self.delegate?.reloadData()
            }
        }
    }
    
  3. 通过您的 Collection View Controller 采用协议(protocol)并添加以下方法:

    func reloadData() {
        DispatchQueue.main.async {
            self.collectionView.reloadData()
        }
    }
    
  4. 更新单元格时设置单元格delegate属性

    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
    }
    

关于ios - 从单元格中的按钮重新加载 Collection View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49612156/

相关文章:

ios - 仅使用 youtube IOS API 的 Youtube 中的视频上传问题新的谷歌帐户

xcode - 如何将 iPad View 选项添加到 Xcode 预览?

swift - 没有 swift 调用 didUpdateHeading

xcode - 访问另一个函数内的对象

objective-c - Xcode - 让单元格在触摸事件后显示新文本,使其展开

ios - 减少 UICollectionView 中的项目间距

ios - 无法使用类型为 '()' 的参数列表调用函数

swift - UIAlertAction 背景颜色

iphone - iOS 在按钮点击时添加 View

ios - 从 imageview 点击手势获取索引或标签值