I want to create a like button on a table view cell, just like Instagram, Facebook, and 100s of other social network apps have, but I am struggling to understand how this can be done properly keeping in mind MVC paradigm.
My structure looks like this:
Model - FeedPost class
View - FeedCell class (UITableViewCell subclass)
Controller - FeedTableViewController class (UITableViewController subclass)
The first thing that came to mind was to do the following:
In FeedCell.swift:
@IBAction func likeButtonPressed(_ sender: AnyObject) {
if let button = sender as? UIButton {
post.like(completed: {
if(completed){
button.isSelected = !button.isSelected
}
})
}
}
And in FeedPost.class:
func like(completed: (Bool) -> Void ) {
//Make a request to a server and when it is done call
completed(true)
}
But this certainly breaks the MVC pattern, as I access my model from the view. So I probably want to work with my data and view via the view controller. The view controller stores the array of posts. So I want to do the following: - Respond to user pressing the button on the table view cell - Find out which post was liked - Perform the server request passing the id of the post or any other reference to it - Upon successful completion of the request, change button state to selected
How would you do this while following the MVC pattern?
Any examples or open source projects where this was done the right way will be highly appreciated.