Reputation: 4100
I'm fairly new to MVC and I have a question regarding models and views. I have some content views that allow users to favorite that content with a tap. This happens in the view and I need to update the data model with the new information. It seems to me the easy approach would be to have a Note
property on each cell and then when that cell was tapped in the controller I could call addFavoriteNote
or removeFavoriteNote
on the DataSource
using the note associated with that cell. However, Note
is a class from the model, so it is my understanding that it would be incorrect to have a Note
property in the UITableViewCell
. Is this wrong in MVC and if so what is an MVC-oriented approach?
class Note: Comparable {
var content: String!
var creationDate: Date!
var author: Person!
init(content: String, creationDate: Date, author: Person) {
self.content = content
self.author = author
self.creationDate = creationDate
}
convenience init(author: Person) {
self.init(content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in efficitur nulla. Suspendisse facilisis tincidunt dictum. Praesent bibendum efficitur lectus vitae imperdiet. Sed odio est, fermentum et turpis vel, pellentesque volutpat magna. Aliquam iaculis libero at dignissim rutrum. Phasellus luctus elementum eros, gravida consequat tellus dignissim ultricies. Ut consectetur ut ante imperdiet molestie. Aliquam blandit, nulla eu egestas accumsan, diam justo varius augue, vel pellentesque felis tortor in orci. In nunc erat, luctus a mauris in, molestie blandit lorem. Vestibulum cursus, ligula sed lobortis vulputate, mauris eros finibus dui, id pretium lacus tortor eu erat. Curabitur pretium, ante in pellentesque vehicula, metus quam interdum leo, faucibus facilisis tellus massa a turpis. Pellentesque sit amet est sem. Vestibulum posuere luctus libero pretium faucibus", creationDate: Date(), author: author)
}
static func <(lhs: Note, rhs: Note) -> Bool {
return lhs.creationDate < rhs.creationDate
}
static func ==(lhs: Note, rhs: Note) -> Bool {
return lhs.creationDate == rhs.creationDate
}
}
class DataSource: NSObject {
private var favorites: [Note] = []
func addFavoriteNote(note: Note) {
favorites.append(note)
}
func removeFavoriteNote(note: Note) {
if let index = favorites.index(of: note) {
favorites.remove(at: index)
}
}
//...more stuff
}
Upvotes: 0
Views: 161
Reputation: 318854
You are correct that you should not have the cell update the model. Your cell should notify the view controller of the event of checking/unchecking. This would be implemented using a delegate pattern or using a closure. The view controller would be the delegate or provide the closure that is called when the event takes place in the cell. As the controller, the view controller can handle this event by updating the model as needed.
A controller provides the view (cell in this case) with just enough data to display itself.
A controller handles events from views (such as a cell) that result in the model being updated.
The controller handles model events and tells views to update based on updated data.
Upvotes: 1