First you must have a UITableView on your storyboard / view controller

AND you must link it up (via the storyboard) in the normal way to your POINTER within the view controller
within the “.m” (main implementation file) :

 @implementation ViewController{
    
    __weak IBOutlet UITableView *tv;
    
}

A UITableView’s job is to display table cells (UITableViewCell) so lets create our own custom tableview cell

Create a new file, for example MyTableCell and make sure its a subclass of UITableViewCell
Also click the create XIB file checkbox


When the view controller is loading … – (void)viewDidLoad

You must load the NIB of your table view cell and let the UITableView know that it exists (register it) :

 - (void)viewDidLoad {
    [super viewDidLoad];
   
    UINib *nib_my_cell = [UINib nibWithNibName:@"MyTableCell" bundle:nil];
    [tv registerNib:nib_my_cell forCellReuseIdentifier:@"MyTableCell"];
    
}

 


The view controller must implement two interfaces … “INTERFACE PROTOCOLS”

Whats an interface ?
An interface is like a contract – it ensures that your view controller will agree to have certain behaviours

Protocols are important because we are working with Classes written by other people.
A protocol is a simple way of saying :
“if you want to use my class … you need to talk to it like this …”

We already know how to get a class to “do something” … we call a function in that class
e.g. we can tell a label to set the text as follows :
[myLabel1 setText:user.name];

But what happens if the other persons class wants to ASK something from our class ??

If we implement a protocol we AGREE to answer certain questions in a certain way.

In practice this means that your view controller will need to have certain functions so ….

At the top of your “.h” (header) file add :

 @interface ViewController : UIViewController <UITableViewDataSource>

As a “UITableViewDataSource” – your view controller will act as a source of data for your table


OK … we’ve said that our view controller is a type of: UITableViewDataSource  … this means it can act as data-source for our table

We need to tell our UITableView that our view controller will be its UITableViewDataSource

So we need to link it in the storyboard … drag from the UITableView to the UIViewController and link the datasource

We’ve also said that our view controller agrees to be a UITableViewDataSource  (like a contract)
… so what does our view controller need to do ????

It needs to implement two functions :

 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

The first function tells the table view how many rows there are in each section of the table :

YOU can implement the logic to allow this to happen .. but the logic MUST result in a number !!!
>>>>

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [myArrayOfUsers count];
}

The second function tells the table what cell to use for each row :

Again you must implement the logic but it must return a  UITableViewCell that has already been setup to display the data you want.

The table view will call your function when its ready and it will pass two things

1) It will pass “itself” to the function (so you know which tableview is requesting the data)
2) It will also pass an index path so you know which row the tableview wants the cell for.

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // get a instance of a reusable cell ... thats based on our custom table view cell
    static NSString *myCellIdentifier = @"MyTableCell";
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:myCellIdentifier];
    
    //Get a user item from our model
    User *user = [myArrayOfUsers objectAtIndex:indexPath.row];
    
    //this next line
    //we can convert the general UITableViewCell to a specific class
    MyTableCell *myCell = (MyTableCell*)cell;
    
    //Setup the cell
    [myCell.myLabel1 setText:user.name];
    [myCell.myLabel2 setText:user.email];
    
    //return the newly setup cell !!!
    return cell;
}