0

I couldn't find an example with good explanation how to include UITableView in my project using MVC pattern. Let's say that at the beginning I have only two files 'MainViewController' (:UIViewController), and 'MainModel' (:NSObject) containing my Array with data for cells.

Where I should have a reference to UITableView object, which file should be delegate for table, ... ?

3
  • 1
    try UITableViewController first Commented Mar 25, 2014 at 19:11
  • unfortunately I want to understand how to divide my code into Model View and Controller, without subclassing UITableViewController :) Commented Mar 25, 2014 at 19:25
  • UITableView - view, knows only about how to draw itself, UIViewController (or UITableViewController as a subclass of) - controller, knows where ans what data needs to be displayed (or refreshed) on managed by it's own view and about user actions, Model - some class (classes mechanism), knows nothing about drawing and fetching, but let's know about it's own state (objects states) changes to interested subscribers. So, why don't you want to subclass UITableViewController? Commented Mar 25, 2014 at 22:37

2 Answers 2

1

Your MainViewController will have a view property that you should point to your UITableView instance. You can have any object be your delegate, but usually your delegate is the view controller that controls it, which would be your MainViewController.

That said, there's a subclass of UIViewController called UITableViewController that you should probably be using as the superclass of MainViewController. It has some automatic functionality for controlling UITableViews. In fact, instantiating a UITableViewController (or any its subclasses) will automatically create a UITableView and point to it in its view & tableView properties.

Sign up to request clarification or add additional context in comments.

Comments

0

So your MainModel is the model. MainViewController is your controller. The tableView is your view. Add it as a property to your MainViewController and set your MainViewController to be the dataSource and delegate for the tableView. Then when you are populating the tableView, use the data in your MainModel. For instance:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[MainModel sharedInstance] myArray].count;
}

Or create an NSArray property on MainViewController and populate it from MainModel in viewDidLoad or viewWillAppear.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.