ios - Firebase swift中的点赞按钮
<p><p>我正在尝试为我的 UITableViewCell 创建一个赞按钮。到目前为止,我设法做的是每次单击按钮时用 +1 更新喜欢的数量。但是,我只希望用户能够按一次按钮,如果同一用户再次单击该按钮,则用户不喜欢该帖子-就像 facebook 喜欢按钮一样。</p>
<p>如果有任何帮助,我将通过 facebook 登录我的应用程序。</p>
<p><strong>我的代码:</strong></p>
<pre><code>@IBAction func likeTapped(sender: AnyObject) {
//print(pathDB)
FIRDatabase.database().reference().child("feed-items").child(pathDB).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
let antalLikes = snapshot.value!["likesForPost"] as! Int
print(antalLikes)
let dataPathen = self.pathDB
print(dataPathen)
self.updateTotalNoOfPost{
print("Updated")
}
// ...
}) { (error) in
print(error.localizedDescription)
}
}
func updateTotalNoOfPost(completionBlock : (() -> Void)){
let prntRef = FIRDatabase.database().reference().child("feed-items").child(pathDB).child("likesForPost")
prntRef.runTransactionBlock({ (resul) -> FIRTransactionResult in
if let dealResul_Initial = resul.value as? Int{
resul.value = dealResul_Initial + 1
//Or HowSoEver you want to update your dealResul.
return FIRTransactionResult.successWithValue(resul)
}else{
return FIRTransactionResult.successWithValue(resul)
}
}, andCompletionBlock: {(error,completion,snap) in
print(error?.localizedDescription)
print(completion)
print(snap)
if !completion {
print("Couldn't Update the node")
}else{
completionBlock()
}
})
}
</code></pre>
<p>这里有我在 firebase 中的结构:
<a href="/image/zwjCb.png" rel="noreferrer noopener nofollow"><img src="/image/zwjCb.png" alt="enter image description here"/></a> </p>
<p>希望你们能帮助我:)</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果你的 JSON 是这样的:-</p>
<pre><code>{
feed-items: {
feedItem1 :{
feedText : "This is feed1",
feedLikes : {uid1 : "true",
uid2 : "true"
}
},
feedItem2 :{
feedText : "This is feed2",
feedLikes : {uid13 : "true",
uid2 : "true"
}
},
feedItem3 :{
feedText : "This is feed4",
feedLikes : {uid4 : "true",
uid10 : "true"
}
},
}
}
</code></pre>
<p>在检索之前,您必须检查用户是否已经喜欢这篇文章,并相应地设置点赞按钮的状态:-</p>
<p>用于存储检索到的字典使用:- </p>
<pre><code>struct feed {
var feedLikes : NSMutableDictionary!
var feedText : String!
var doILikeThisPost : Bool!
var feedNameI : String!
init(likes:NSMutableDictionary!, feed : String!, likeTisPost : Bool!, feedNM : String!){
self.doILikeThisPost = likeTisPost
self.feedLikes = likes
self.feedText = feed
self.feedNameI = feedNM
}
}
</code></pre>
<p>在你的 tableViewController 中:- </p>
<pre><code>import UIKit
import Firebase
class customTableViewController : UIViewController, UITableViewDelegate ,UITableViewDataSource{
var feedD = ()
@IBOutlet wear var customTableView : UITableView!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
customTableView.delegate = self
customTableView.dataSource = self
retrieveTheData()
}
func retrieveTheData(){
FIRDatabase.database().reference().child("feed-items").observeSingleEventOfType(.Value, withBlock: {(allFeeds) in
if let feedDict = allFeeds.value as? {
for each in feedDict{
if let feedLikesDict = each.1["feedLikes"] as? NSMutableDictionary{
if feedLikesDict != nil{
let temp = feed.init(likes: feedLikesDict, feed: each.1["feedText"] as! String, likeTisPost: true, feedNM : each.0)
self.feedD.insert(temp, atIndex: 0)
self.customTableView.reloadData()
}else{
let temp = feed.init(likes: feedLikesDict, feed: each.1["feedText"] as! String, likeTisPost: false, feedNM : each.0)
self.feedD.insert(temp, atIndex: 0)
self.customTableView.reloadData()
}
}else{
let temp = feed.init(likes: nil, feed: each.1["feedText"] as! String, likeTisPost: false, feedNM : each.0)
self.feedD.insert(temp, atIndex: 0)
self.customTableView.reloadData()
}
}
}
})
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return feedD.count ?? 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.customTableView.dequeueReusableCellWithIdentifier("CELL") as! customCell
if feedD.doILikeThisPost == true{
cell.feedLikeBtn.selected = true
cell.feedTextPost = feedD.feedText
cell.feedName = feedD.feedNameI
cell.parentTableViewController = self
cell.indexPathForRow = indexPath.row
}else{
cell.feedLikeBtn.selected = false
cell.feedTextPost = feedD.feedText
cell.feedName = feedD.feedNameI
cell.parentTableViewController = self
cell.indexPathForRow = indexPath.row
}
return cell
}
}
</code></pre>
<p>你自定义的TableViewCell:- </p>
<pre><code>class customCell : UITableViewCell{
@IBOutlet weak var feedLikeBtn : UIButton!
var feedTextPost = String()
var feedName = String()
var indexPathForRow : Int!
var parentTableViewController : customTableViewController!
@IBAction func likeBtn(sender:UIButton!){
if sender.selected == false{
FIRDatabase.database().reference().child("feed-items").child(feedName).child("feedLikes").updateChildValues()
self.parentTableViewController.feedD.feedLikes.setObject("true", forKey: currentUserID)
self.parentTableViewController.feedD.doILikeThisPost = true
self.parentTableViewController.customTableView.reloadData()
}else if sender.selected == true{
FIRDatabase.database().reference().child("feed-items").child(feedName).child("feedLikes").child(currentUserID).removeValue()
self.parentTableViewController.feedD.feedLikes.removeObjectForKey(currentUserID)
self.parentTableViewController.feedD.doILikeThisPost = false
self.parentTableViewController.customTableView.reloadData()
}
}
}
</code></pre>
<p>另见:- <a href="https://stackoverflow.com/a/39458044/6297658" rel="noreferrer noopener nofollow">https://stackoverflow.com/a/39458044/6297658</a> </p></p>
<p style="font-size: 20px;">关于ios - Firebase swift中的点赞按钮,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39489362/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39489362/
</a>
</p>
页:
[1]