You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift/ObjC中MVC模式疑问:UITableViewCell与ViewController该如何归类?

MVC Directory Structure: Where to Put UITableViewCell and ViewController?

Great question—this is one of those MVC organizational gray areas that trips up a lot of iOS devs, especially when starting out! Let’s break this down clearly based on MVC principles and common iOS development practices.

Core MVC Roles First

To ground this, let’s quickly recap the core responsibilities of each layer (since this is where most confusion starts):

  • Model: Handles data, business logic, and data persistence—no UI code belongs here
  • View: Focused entirely on presenting visual elements and capturing user input (think UIKit components, custom views, and yes, table view cells)
  • Controller: Acts as the middleman between Model and View—manages data flow, user interactions, and view lifecycle

Where to Place UITableViewCell?

Short answer: Absolutely put your custom UITableViewCell classes in the View directory (or a subdirectory like Views/Cells if you want to keep cell types grouped neatly).

Here’s the reasoning: A UITableViewCell is a View component through and through. Its only job is to render data into a visual layout, handle cell-specific UI interactions (like button taps, though those should usually bubble up to the controller), and nothing else. It shouldn’t contain business logic or directly manage data models beyond what’s needed to display content.

Many teams opt for a dedicated Cells subfolder under Views to avoid clutter, especially if the app has dozens of custom cell types. That’s a perfectly valid tweak—what matters most is consistency across your project.

Where to Place ViewController?

Another clear yes: UIViewController subclasses belong in the Controller directory (most iOS projects use ViewControllers as the folder name for clarity).

View controllers are the classic "Controller" layer in iOS MVC. They fetch data from models, configure views (including setting up table view cells), respond to user actions, and manage the overall screen lifecycle. Even though "View" is in their name, their primary role is coordinating between the model and view layers—so they fit right in with other controller components.

A Quick Note on Flexibility

While sticking to these guidelines keeps your project aligned with MVC principles, don’t be afraid to adapt based on your team’s workflow or project size:

  • For small projects, some devs skip strict directory separation and organize files by feature (e.g., all files related to a "UserProfile" screen in one folder). This works as long as you still respect MVC’s role boundaries.
  • For larger apps, a feature-based folder structure (e.g., a UserProfile folder with its own View, Controller, and Model subfolders) can make navigation easier and keep related code together.

Final Takeaway

Stick to the core MVC roles, and you can’t go wrong:

  • Custom UITableViewCell → View (or Views/Cells)
  • UIViewController → Controllers (or ViewControllers)

This keeps your codebase predictable, easy to navigate, and true to MVC’s original intent.


内容的提问来源于stack exchange,提问作者KirillC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:57:20