title: How to create a very basic UITableView programmatically with dynamic cell height and auto layout?
body: <p>Please note: I know there are quite a few examples on similar lines. I am looking for the most basic solution with the minimum setup required. </p>
  
  <p>I was trying to create one on my own, and I know I am way off.. </p>
  
  <pre><code>    #import "ViewController.h"
  
      @interface ViewController () &lt;UITableViewDataSource, UITableViewDelegate&gt;
  
      @property(strong, nonatomic) UITableView *tableView;
      @property(strong, nonatomic) NSMutableArray *dataArray;
      @property (strong, nonatomic) UITableViewCell *customCell;
  
      @end
  
      @implementation ViewController
  
      - (void)viewDidLoad {
          [super viewDidLoad];
          // Do any additional setup after loading the view, typically from a nib.
          self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
          [self.tableView setDataSource:self];
          [self.tableView setDelegate:self];
          [self.tableView setShowsVerticalScrollIndicator:NO];
          self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
          self.tableView.rowHeight = UITableViewAutomaticDimension;
          [self.view addSubview:self.tableView];
  
          self.dataArray = [@[@"For the past 33 years, I have looked in the mirror every morning and asked myself: 'If today were the last day of my life, would I want to do what I am about to do today?' And whenever the answer has been 'No' for too many days in a row, I know I need to change something. -Steve Jobs",
                           @"Be a yardstick of quality. Some people aren't used to an environment where excellence is expected. - Steve Jobs",
                           @"Innovation distinguishes between a leader and a follower. -Steve Jobs"] mutableCopy];
  
  
      }
  
      - (void)didReceiveMemoryWarning {
          [super didReceiveMemoryWarning];
          // Dispose of any resources that can be recreated.
      }
  
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  
          return [self.dataArray count];
      }
  
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          NSString *cellIdentifier = @"CustomCell";
          UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  
          if (cell == nil)
          {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
          }
  
          cell.backgroundColor = [UIColor colorWithRed:249.0/255 green:237.0/255 blue:224.0/255 alpha:1.0];
  
          int dataIndex = (int) indexPath.row % [self.dataArray count];
          cell.textLabel.text = self.dataArray[dataIndex];
          cell.textLabel.numberOfLines = 0;
          cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
  
          NSDictionary *views = @{@"label":cell.textLabel};
          NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label]|"
                                                                         options:0
                                                                         metrics:nil
                                                                           views:views];
          [cell.contentView addConstraints:constraints];
  
          constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label]|"
                                                                options: 0
                                                                metrics:nil
                                                                  views:views];
          [cell.contentView addConstraints:constraints];
  
          return cell;
  
      }
  
      - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
          // Calculate a height based on a cell
          if(!self.customCell) {
              self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
          }
  
          // Configure the cell
          int dataIndex = (int) indexPath.row % [self.dataArray count];
          self.customCell.textLabel.text = self.dataArray[dataIndex];
  
          // auto layout
  
          NSDictionary *views = @{@"label":self.customCell.textLabel};
          NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label]|"
                                                                         options:0
                                                                         metrics:nil
                                                                           views:views];
          [self.customCell.contentView addConstraints:constraints];
  
          constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label]|"
                                                                options: 0
                                                                metrics:nil
                                                                  views:views];
          [self.customCell.contentView addConstraints:constraints];
  
          // Layout the cell
  
          [self.customCell layoutIfNeeded];
  
          // Get the height for the cell
  
          CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
  
          // Padding of 1 point (cell separator)
          CGFloat separatorHeight = 1;
  
          return height + separatorHeight;
      }
  
      - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
  
          return 140;
  
      }
  
  
      @end
  </code></pre>