如何将MVVM架构的iOS事件列表代码转换为MVC架构?
Hey there! I totally get the confusion—MVC implementations can feel all over the place in iOS resources, especially when you’re coming from MVVM. Let’s walk through exactly what changes you need to make to shift your existing code over to a clean, standard MVC setup:
1. Ditch the EventViewModel entirely
The ViewModel in MVVM acts as a middleman between your data and UI, but in MVC, that responsibility moves straight to the EventViewController. Here’s how to handle it:
- Move all data-fetching logic (like API calls or local data loading) from the ViewModel into the ViewController. For example, if your ViewModel had a
fetchEvents(completion:)method, copy that code into a private function inEventViewController. - Transfer any UI-focused data transformation (like formatting dates for display, or calculating event statuses) to the ViewController too. Instead of the ViewModel providing a pre-formatted date string, write a helper method in the ViewController like
private func formatEventDate(_ date: Date) -> Stringand call it when configuring cells.
2. Merge EventDataSourceModel logic into the ViewController
Your DataSourceModel is probably handling table view data management—in MVC, this work lives with the ViewController (or you can keep it as a helper object owned by the ViewController, if you want to keep code clean):
- If your DataSourceModel was feeding data to the ViewModel, now have the ViewController own the array of
EventModelobjects directly. - Update your table view’s
UITableViewDataSourcemethods (numberOfRowsInSection,cellForRowAt) to pull data straight from the ViewController’s event array. For example:func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return events.count // `events` is a [EventModel] property in the ViewController }
3. Update EventViewController to own the full workflow
Now your ViewController becomes the central coordinator:
- It fetches data, stores it, transforms it for display, and tells the table view to reload when data changes.
- It directly configures table view cells instead of passing ViewModel objects to them. For example:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "EventCell", for: indexPath) as! EventCell let event = events[indexPath.row] cell.titleLabel.text = event.title cell.dateLabel.text = formatEventDate(event.date) cell.locationLabel.text = event.location return cell }
4. Simplify EventCell
Your cell no longer needs to interact with a ViewModel—make it accept raw data or a EventModel directly:
- Add a configuration method to the cell that takes an
EventModel(or individual properties) and sets up its UI elements. For example:class EventCell: UITableViewCell { @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var dateLabel: UILabel! @IBOutlet weak var locationLabel: UILabel! func configure(with event: EventModel) { titleLabel.text = event.title dateLabel.text = DateFormatter.localizedString(from: event.date, dateStyle: .medium, timeStyle: .short) locationLabel.text = event.location } } - Then call this method from the ViewController’s
cellForRowAtinstead of setting properties directly.
5. Keep EventModel as-is (for the most part)
Your EventModel is a pure data object—this stays exactly the same in MVC! The only exception: if you had computed properties in the model that were focused on UI display (like a formattedDate string), move those to the ViewController as helper methods instead.
Quick Note on MVC Variations
Don’t stress if you see different MVC implementations online—at its core, MVC just means:
- Model: Holds data and business logic (no UI code)
- View: Displays UI and handles user input (no data logic)
- Controller: Connects the two—fetches data, transforms it for the view, updates the view when data changes, and handles user actions.
内容的提问来源于stack exchange,提问作者brucecraigb

