如何使用协议更新我的collectionView?

时间:2019-01-30 06:57:05

标签: swift firebase uicollectionview uibutton

我正在使用协议,以便能够访问设置在单元格中的按钮,除了点击按钮时不刷新图像,其他所有操作都在进行。

我在执行功能后已经尝试重新加载集合视图,并且无法正常工作,我也尝试重新加载单个单元格,但仍然没有结果。我觉得我错过了一些小东西,或者我做错了所有事情。

这是我的协议中访问单元格按钮的功能

func didTapVoteUp(for cell: HomeFeedCell, vibe: PostVibe) {
    print("Tap Up Vote From HomeFeedController")

    guard let city = CityState.currentCity else { return }
    guard let indexPath = collectionView.indexPath(for: cell) else { return }
    let vibe = self.postVibe[indexPath.item]
    fetchUpVote(userPostId: vibe.userPostId, postId: vibe.postId, city: city)

    guard let uid = Auth.auth().currentUser?.uid else { return }
    let values = [uid : 1]
    Database.database().reference().child("votes").child(vibe.postId).updateChildValues(values) { (err, _) in
        if let err = err {
            print("Failed to vote for post: ", err)
            return
        }
        print("Succesfully vote for the post")
        //Here is where I tried the collectionView.reloadData
        //and collectionView.reloadItems(at: [indexPath]) not working either
    }
}

我创建了另一个单元格,该单元格可以获取数据以更新信息,但是仍然无法使用collectionView.reloadData()

func fetchUpVote(userPostId: String, postId: String, city: String) {
    let ref = Database.database().reference().child("vibes").child("city").child(city).child(userPostId).child(postId)
    ref.observeSingleEvent(of: .value, with: { (snapshot) in

        guard let dictionary = snapshot.value as? [String: Any] else { return }
        let updatePost = PostVibe(userPostId: userPostId, postId: postId, dictionary: dictionary)

        let votes = updatePost.vote
        let currentTime = updatePost.limitPostTime
        let newLimitTime = currentTime.addingTimeInterval(60.0 * 30.0).timeIntervalSince1970

        let values = ["limitPostTime" : newLimitTime, "votes" : votes + 1 ] as [String : Any]

        let updateRef = Database.database().reference().child("vibes").child("city").child(city).child(userPostId).child(postId)
        updateRef.updateChildValues(values, withCompletionBlock: { (err, reference) in
            if let err = err {
                print("Failed to update values: ", err)
                return
            }
            print("Successfully updated it values: ", values)
            //This doesn't work either
            self.collectionView.reloadData()
        })
    })
    { (err) in
        print("Failed to fetch postIdWithUserId: ", err)
    }
}

import UIKit

protocol HomePostDelegate {
func didTapVoteUp(for cell: HomeFeedCell, vibe: PostVibe)
func didTapVoteDown(for cell: HomeFeedCell, vibe: PostVibe)
}

class HomeFeedCell: UICollectionViewCell {

var vibe: PostVibe? {
    didSet {
        guard let imageUrl = vibe?.imageUrl else { return }
        placeView.loadImage(urlString: imageUrl)

        guard let profileImageUrl = vibe?.creatorImageUrl else { return }
        vibeView.userPhotoView.loadImage(urlString: profileImageUrl)
        vibeView.creatorName.text = vibe?.creatorName

        if vibe?.hasVoteUp == true && vibe?.hasVoteDown == false {
            vibeView.voteUpButton.setImage(#imageLiteral(resourceName: "upVoteOn"), for: .normal)
            vibeView.voteDownButton.setImage(#imageLiteral(resourceName: "downVoteOff"), for: .normal)

            vibeView.voteUpButton.isEnabled = false
            vibeView.voteDownButton.isEnabled = true

        } else if vibe?.hasVoteDown == true && vibe?.hasVoteUp == false{
            vibeView.voteDownButton.setImage(#imageLiteral(resourceName: "downVoteOn"), for: .normal)
            vibeView.voteUpButton.setImage(#imageLiteral(resourceName: "upVoteOff"), for: .normal)

            vibeView.voteUpButton.isEnabled = true
            vibeView.voteDownButton.isEnabled = false

        }

        guard let voteNumber = vibe?.vote else { return }
        vibeView.votesDisplay.text = "\(voteNumber)"

    }
}
}

我期望的是从.setImage中无法使用的按钮自动更新HomeFeedCell

更新此图像的唯一方法是手动刷新collectionView或关闭应用程序。

0 个答案:

没有答案