Youtube 风格的投票。这是什么逻辑?

Youtube style voting poll. what is the logic?

您好,我正在尝试让我的投票类似于 Youtube。现在,当用户点击 'like' 按钮时,它会上升,如果同一个用户点击它,它就会下降。我的问题是制作不喜欢按钮的最佳方法是什么 "when the user likes the button and then clicks on the dislike button, the counters get calculated correctly and vice versa?" 谢谢!

**附加信息:我还有一个不喜欢按钮和一个不喜欢标签。

cell!.shouldEnableLikeButton(false)

let liked: Bool = cell!.likeButton.selected
cell?.setLikeStatus(liked)

let originalButtonTitle = cell?.likeLabel!.text

var likeCount: Int = originalButtonTitle!.toInt()! 

if liked {
    likeCount += 1
} else {
    likeCount -= 1
}

cell!.likeLabel.text = "\(likeCount)"

我假设你有一个后端接受一个数字来修改计数。

在 phone 上本地存储 "poll" 的 likeCount。将每个民意调查保留为唯一 ID,并在 phone 数据库中保留用户已对该民意调查投票的内容的映射。 它可以有三种状态:

  1. 没有
  2. 尚未投票

现在,如果本地状态从“是”变为“否”,应用会向后端报告 -2。如果状态从否变为是,应用会报告 +2。如果状态从 NotYetVoted 变为 Yes,应用报告 +1,NotYetVoted 变为 No,应用报告 -1。在所有其他情况下,应用程序不会向后端报告任何内容。

希望对您有所帮助。