【问题标题】:Firebase like system inside tableview cell表格视图单元内的类似 Firebase 的系统
【发布时间】:2017-02-17 23:43:49
【问题描述】:

我在我的 tableview 单元格中创建了类似的系统。但是它有问题。

  1. 如果我喜欢一件事,每 7 个单元格也越来越喜欢,为什么? reusableCell 有什么用吗?
  2. 最好的方法是什么,我做错了吗?

这是like按钮系统:

cell.likeButton.addTarget(self, action: #selector(self.tapped), for: .touchUpInside)
    func tapped(sender: DOFavoriteButton) {
            if sender.isSelected {
                // deselect
                sender.deselect()//+1 like
            } else {
                // select with animation
                sender.select()//-1 like 
            }
        }

这是我的like系统函数:

func likeSystem(sender: DOFavoriteButton, cellForRowAt indexPath: IndexPath){
        if sender.isSelected {
        let cell = tableView.dequeueReusableCell(withIdentifier: "snusProductsCell", for: indexPath) as! SnusProductTableViewCell
        self.databaseRef.child("Snuses").child(self.products[indexPath.row].snusProductTitle).runTransactionBlock({
            (currentData:FIRMutableData!) -> FIRTransactionResult in
            if var post = currentData.value as? [String : AnyObject], let uid = FIRAuth.auth()?.currentUser?.uid {
                var stars : Dictionary<String, Bool>
                stars = post["hasLiked"] as? [String : Bool] ?? [:]
                var starCount = post["likes"] as? Int ?? 0
                if let _ = stars[uid] {
                    // Unstar the post and remove self from stars
                    starCount -= 1
                    stars.removeValue(forKey: uid)

                } else {
                    // Star the post and add self to stars
                    starCount += 1

                    stars[uid] = true
                    sender.deselect()
                }
                post["hasLiked"] = starCount as AnyObject?
                post["likes"] = stars as AnyObject?

                // Set value and report transaction success
                currentData.value = post

                return FIRTransactionResult.success(withValue: currentData)
            }
            return FIRTransactionResult.success(withValue: currentData)
        }) { (error, committed, snapshot) in
            if let error = error {
                print(error.localizedDescription)
            }
        }
        }else{
            sender.select()
        }
    }

我的大脑正在崩溃 ATM.. 不知道如何继续。请带我回到赛道。

这是我的结构:

【问题讨论】:

  • 这与 [您的上一个问题](stackoverflow.com/q/39937930) 中的代码相同。使用完全相同的代码打开多个问题通常表明您应该创建MCVE 来隔离问题。
  • 哥们,你的问题有什么消息吗?
  • 我实际上被 indexPath.rows 困住了,因为我不得不在 tableview 函数之外使用那些,所以我放弃了。但我计划今天阅读您的问题并接受它:)
  • ooooookay,结果如何? :P

标签: ios swift firebase tableview firebase-realtime-database


【解决方案1】:

这是我的功能,可以解决喜欢的问题:

class Cell: UITableViewCell {

    var liked = 0
    var likes: [String] = []
    var likeCounter = 0

    func readLikeStatus() {
        if liked == 0 {
            likeButton.setImage(UIImage(named: "unlike"), forState: .Normal)
            likeButton.setTitle("", forState: .Normal)
            likeLabel.text = "\(likeCounter) Likes"

        } else {
            likeButton.setImage(UIImage(named: "like"), forState: .Normal)
            likeButton.setTitle("", forState: .Normal)
            likeLabel.text = "\(likeCounter) Likes"
        }           
    }

    @IBAction func likeButton(sender: AnyObject) {

        if liked == 0 {

            likeTweet()
            liked = 1
            likeCounter = likeCounter + 1
            readLikeStatus()

        } else if liked == 1 {

            unlikeTweet()
            liked = 0
            likeCounter = likeCounter - 1
            readLikeStatus()

        }
    }

    func likeTweet() {
        let userID = [backendless.userService.currentUser.objectId: backendless.userService.currentUser.objectId]
        let usersRef = firebase.child("DeejayTweets").child(passedDJ.objectId).child(tweetID).child("likes")
        usersRef.updateChildValues(userID)
    }

    func unlikeTweet() {
        let userID = backendless.userService.currentUser.objectId
        let usersRef = firebase.child("DeejayTweets").child(passedDJ.objectId).child(tweetID).child("likes")
        usersRef.child(userID).removeValue()
    }

    func bindData(post: Tweet, indexPath: NSIndexPath, commentCount: NSMutableDictionary, avatare: NSMutableDictionary) {
        likeButton.setImage(UIImage(named: "unlike"), forState: .Normal)
        likeButton.setTitle("", forState: .Normal)
        liked = 0

        setLike(post, indexPath: indexPath, commentCount: commentCount, avatare: avatare)
     }

     func setLike(post: Tweet, indexPath: NSIndexPath, commentCount: NSMutableDictionary, avatare: NSMutableDictionary) {

        //SET IF TWEET IS LIKED
        if post.likes.contains(backendless.userService.currentUser.objectId) {
            likeButton.setImage(UIImage(named: "like"), forState: .Normal)
            likeButton.setTitle("", forState: .Normal)
            liked = 1
         }

         //SET LIKE COUNTER


         if post.likes.count == 1 {
            likeLabel.text = "0 Likes"
            likeCounter = 0
         } else if post.likes.count == 1 {
            likeLabel.text = "\(post.likes.count - 1) Like"
            likeCounter = 1
         } else {
            likeLabel.text = "\(post.likes.count - 1) Likes"
            likeCounter = 1
        }
     }

调用一切:

class ViewController: UIViewController.... {

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! Cell
            let post = tweets[indexPath.row]

            cell.bindData(post, indexPath: indexPath, commentCount: commentCount, avatare: avatare)

            return cell
    }

在我的火力基地中,我处理的一切都是这样的:

我并不是说这是世界上最好的做法,但它正在发挥作用,并且可能会让您了解如何处理这个问题。

【讨论】:

  • 你能说,为什么 [backendless.userService.currentUser.objectId: backendless.userService.currentUser.objectId] 在数组中?而passedDJ.objectId是post id吗?
  • 所以我有价值和关键的 id,我想要这样。你也可以把名字:id
  • 我在图像上有这个 JSON 结构。所以它应该是关键?
  • 我喜欢的所有需要​​是将“喜欢”保存为[key:id and value:id] 的数组,这样我就可以最快地访问它。我可以计算喜欢的人的数量,我可以有一个 where 子句通过“allvalues”获取所有用户的 ID
  • 我想问你一件事。为什么你为帖子做这样的结构?就像在 UID 中存储帖子一样?以后有什么目的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
相关资源
最近更新 更多