-1

I'm using a TableView to create a chat app. Where each cell represent a msg. The cell height change based on the contents of the msg. For example if its a photo it will have a fixed size. If its a text it will have a size depend of number of line in msg. Also the whole TableView change size when the user try to send a msg based on the size of the keyBoard appeared.

The problem is that when the tableView size changes, the tableView cells get messed up! This only happen when is scroll down (re-render the cells that disappear)

Update: This happened also if some of the cells where invisible (big tableView). before the re-sizing. And also it does not have anything with the type. Because it does the same if the msgs are all text

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

  if ([[[msg objectAtIndex:indexPath.row] objectForKey:@"type"] isEqualToString:@"msg"]) {
      int numberOfline= [[[msg objectAtIndex:indexPath.row] objectForKey:@"line"] intValue];
      return topMergeH + topBubbleH + bottomBubbleH + bottomMergeH + (bubbleHieght*numberOfline);
  }

  else 
      return  topMergeH + topBubbleH + bottomBubbleH + bottomMergeH + bubbleHieght + 220;

}


- (void) changeTextFieldPoistion {

   //TextFieldHight = 30
   [_typeRef setFrame:
    CGRectMake(0,self.view.frame.size.height-keyBoardHeight-30
               , _typeRef.frame.size.width, _typeRef.frame.size.height)];
   [_myTableView setFrame:
    CGRectMake(_myTableView.frame.origin.x, _myTableView.frame.origin.y
               , 374, self.view.frame.size.height-_myTableView.frame.origin.y-keyBoardHeight-30)];
}

Pic1: Before Re-sizing Pic2: After Re-sizing 1

2 Answers2

0

I found the answer here

The problem was with the Cell Identifier

Before Code:

static NSString *simpleTableIdentifier = @"ChatSendTableViewCell";
ChatSendTableViewCell *cell =
(ChatSendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:simpleTableIdentifier owner:self options:nil];
    cell = [nib objectAtIndex:0];

}

After Code:

static NSString *CellIdentifier1 = @"ChatSendTableViewCell";
UITableViewCell *cell;

if (indexPath.section == 0)
{
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1];
    }
    else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    }

}
0

1 - when keyboard Appers Get keyboard size -

 keyboardInfo = [notification userInfo];
 kbSize = [[keyboardInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

2-if your chatTable Contains Any row -

whwre [messageList] is Number of Rows

if ([messageList count]) {

    NSIndexPath *iPath = [NSIndexPath indexPathForRow:[messageList count]-1 inSection:0];

    UITableViewCell *lastCell = [chatTable cellForRowAtIndexPath:iPath];

    CGRect aRect = self.view.frame;

    aRect.size.height -= kbSize.height;

    if (!CGRectContainsPoint(aRect, lastCell.frame.origin) ) {

        CGRect bkgndRect = lastCell.superview.frame;

        bkgndRect.size.height += kbSize.height;

        [lastCell.superview setFrame:bkgndRect];

        [chatTable setContentOffset:CGPointMake(0.0,(lastCell.frame.origin.y+lastCell.frame.size.height) - (kbSize.height)) animated:NO];
    }

}