If your JSON is something like this:-
{
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"
}
},
}
}
Before retrieving you will have to check if the user had already liked this post, and set the state of the like button accordingly:-
For storing the retrieved dictionary use:-
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
}
}
In your tableViewController:-
import UIKit
import Firebase
class customTableViewController : UIViewController, UITableViewDelegate ,UITableViewDataSource{
var feedD = [feed]()
@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? [String: AnyObject]{
for each in feedDict{
if let feedLikesDict = each.1["feedLikes"] as? NSMutableDictionary{
if feedLikesDict[currentUerID] != 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[indexPath.row].doILikeThisPost == true{
cell.feedLikeBtn.selected = true
cell.feedTextPost = feedD[indexPath.row].feedText
cell.feedName = feedD[indexPath.row].feedNameI
cell.parentTableViewController = self
cell.indexPathForRow = indexPath.row
}else{
cell.feedLikeBtn.selected = false
cell.feedTextPost = feedD[indexPath.row].feedText
cell.feedName = feedD[indexPath.row].feedNameI
cell.parentTableViewController = self
cell.indexPathForRow = indexPath.row
}
return cell
}
}
You customTableViewCell:-
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([currentUserID : "true"])
self.parentTableViewController.feedD[self.indexPathForRow].feedLikes.setObject("true", forKey: currentUserID)
self.parentTableViewController.feedD[self.indexPathForRow].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[self.indexPathForRow].feedLikes.removeObjectForKey(currentUserID)
self.parentTableViewController.feedD[self.indexPathForRow].doILikeThisPost = false
self.parentTableViewController.customTableView.reloadData()
}
}
}
Also see :-https://stackoverflow.com/a/39458044/6297658