title: create a simple chat view in ios
body: <p>I need to create a simple chat view in which i can show messages from two ends (sender and receiver) like any Message App . </p>
  
  <p>What i have done till now is , created a <code>UITableView</code> , A <code>UIButton</code> and a <code>UITextField</code>. And on that UIButton tap , i am adding UITextField text to array , Now I need the second end also like in ours Messaging app (sender side).</p>
  
  <p>Left side is receiver and Right side is sender.</p>
  
  <p>My app till now looks like</p>
  
  <p><a href="https://i.stack.imgur.com/rlfQn.png" rel="noreferrer"><img src="https://i.stack.imgur.com/rlfQn.png" alt="enter image description here"></a>  </p>
  
  <p><strong>Here is my code:</strong></p>
  
  <pre><code> - (UITableViewCell *)tableView:(UITableView *)tableView
       cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  static NSString *CellIdentifier = @"Cell";
  
  messageLabel = nil;
  
  UITableViewCell *cell = [tableView
                           dequeueReusableCellWithIdentifier:CellIdentifier];
  
  if (cell == nil) {
  
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  
      messageLabel = [[UILabel alloc] init];
      messageLabel.tag = 101;
      [cell.contentView addSubview: messageLabel];
  
        } else {
  
  
      messageLabel = (UILabel*)[cell.contentView viewWithTag: 101];
  
        }    //---calculate the height for the label---
  int labelHeight = [self labelHeight:[listOfMessages objectAtIndex:indexPath.row]];
  labelHeight -= bubbleFragment_height;
  if (labelHeight&lt;0) labelHeight = 0;
  
  messageLabel.frame =
  CGRectMake(bubble_x + 10, bubble_y + 5,
             (bubbleFragment_width * 3) - 25,
             (bubbleFragment_height * 2) + labelHeight - 10);
  
  messageLabel.font = [UIFont systemFontOfSize:15];
  messageLabel.textAlignment = NSTextAlignmentCenter;
  messageLabel.textColor = [UIColor darkTextColor];
  messageLabel.backgroundColor = [UIColor greenColor];
  messageLabel.numberOfLines = 0;
  messageLabel.layer.cornerRadius = 8;
  messageLabel.layer.masksToBounds = YES;
  
  messageLabel.text = [listOfMessages objectAtIndex:indexPath.row];
  [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  
  return cell;
  }
  
  -(void) sendAction:(id) sender {
  [listOfMessages addObject:field.text];
  
  [chatTable reloadData];
  field.text = @"";
  
  [field resignFirstResponder];
   }
  </code></pre>