Home Correct MVC approach for UITableViewCell updating model?
Reply: 0

Correct MVC approach for UITableViewCell updating model?

user6012
1#
user6012 Published in September 20, 2018, 4:37 pm

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
}
share|improve this question

1 Answer 1

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.360246 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO