How to create a basic UITableView

File -> New -> Project -> Single View Application
Open storyboard -> Drag "Table View" to View Controller
Press crtl and drag line from "Table View" to ViewController.h

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property NSMutableArray *arrDataSource;


NSArray: static array
Cannot be changed after the array has been initialized.

NSMutableArray: dynmaic array
Can be modified after they have been created.
Here we used NSMutableArray.

Added two protocols:
UIViewController should be added UITableViewDelegate/UITableViewDataSource protocols.

#import "ViewController.h"

@interface ViewController ()


@implementation ViewController

- (void)viewDidLoad
  [super viewDidLoad];

  _arrDataSource = [[NSMutableArray alloc] init];
  for (int i=0; i<100; i++) {
    [_arrDataSource addObject:[NSString stringWithFormat:@"index:%d", i]];

  self.tableView.delegate = self;
  self.tableView.dataSource = self;

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

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

  static NSString *cellIdentifier = @"MyReuseCellIdentifier";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

  cell.textLabel.text = [_arrDataSource objectAtIndex:indexPath.row];
  return cell;

- (void)didReceiveMemoryWarning
  [super didReceiveMemoryWarning];


We just declared the arrDataSource by using "@property" in ViewController.h and no longer need to "@synthesize" in ViewController.m file.
@synthesize arrDataSource = _arrDataSource;
The compiler will auto generate getter/setter, now we can use _arrDataSource.

// Code generated in background, doesn't actually appear in your application
- (NSMutableArray *)arrDataSource {
  return _arrDataSource;

- (void) setArrDataSource:(NSMutableArray *)arrDataSource {
  _arrDataSource = arrDataSource;


  • initial arrDataSource
  • set delegate = self
  • set dataSource = self (not to arrDataSource)

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

  • number of row in the section

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

  • create reusable cells or a custom cell using tags
  • set cell data

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

  • Called after the user changes the selection

enjoy it~~