【问题标题】:how to store counter variables in Parse如何在 Parse 中存储计数器变量
【发布时间】:2015-11-03 17:52:21
【问题描述】:

以下是我的代码。我有 3 个变量(likeCountdislikeCountVote)需要存储在“Like”类中。我还有一个用户指针,并在“喜欢”类中发布。我将如何存储这些变量?谢谢!!

表格视图控制器:

class UserFeedTableViewController: PFQueryTableViewController {
    var shouldReloadOnAppear: Bool = true

    // Votes: -1 for dislike, 1 for like, 0 for no vote
    var vote: Int = 0 // initialize to user's existing vote, retrieved from the server
    var likeCount: Int = 0 // initialize to existing like count, retrieved from the server
    var dislikeCount: Int = 0 // initialize to existing dislike count, retrieved from the server

    @IBAction func dislikeButton(sender: UIButton) {

    buttonWasClickedForVote(-1)

    self.tableView.reloadData()
    print(likeCount)
    print(dislikeCount)
}

@IBAction func likeButton(sender: UIButton) {

    buttonWasClickedForVote(1)

    self.tableView.reloadData()
    print(likeCount)
    print(dislikeCount)      
}

private func buttonWasClickedForVote(buttonVote: Int) {
    if buttonVote == vote {
        // User wants to undo her existing vote.
        applyChange(-1, toCountForVote: vote)
        vote = 0
    }
    else {
        // User wants to force vote to toggledVote.
        // Undo current vote, if any.
        applyChange(-1, toCountForVote: vote)

        // Apply new vote.
        vote = buttonVote
        applyChange(1, toCountForVote: vote)
    }
}

private func applyChange(change: Int, toCountForVote vote: Int ) {
    if vote == -1 { dislikeCount += change }
    else if vote == 1 { likeCount += change }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath      indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
    let CellIdentifier = "Cell"

    var cell: UserFeedCell? = tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath) as? UserFeedCell

    if cell == nil {
        cell = UserFeedCell(style: UITableViewCellStyle.Default, reuseIdentifier: CellIdentifier)

    }

    if object != nil {
        cell!.likeButton.selected = vote == 1

        cell!.dislikeButton.selected = vote == -1
        cell!.likeButton.setTitle("\(likeCount)", forState: .Normal)
        cell!.dislikeButton.setTitle("\(dislikeCount)", forState: .Normal)

        }

    return cell
}

表格视图单元格:

class UserFeedCell: PFTableViewCell {

    @IBOutlet weak var likeButton: UIButton!

    @IBOutlet weak var dislikeButton: UIButton!

    var post: PFObject? {
        didSet{   
        }
    } 
}

【问题讨论】:

    标签: ios swift parse-platform counter


    【解决方案1】:

    创建一个“Like”类的PFObject,并使用PFObject增量方法将它们改变适当的数量,然后保存该对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 2018-02-08
      相关资源
      最近更新 更多