Madhu Madhu - 19 days ago 5
iOS Question

UITableview with two different custom cells overlapping while scrolling

I have a

UITableView
with two different custom cells. One will show normal text message and other with some text and image. I am able to use two different custom cells in
cellForRowAtIndexPath
and able to see two different cells like below
enter image description here

But whenever i scroll the tableview then the cells are overlapping like below.

enter image description here

Below is my code

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
//minimum size of your cell, it should be single line of label if you are not clear min. then return UITableViewAutomaticDimension;

return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[TicketTableViewCell class]]) {
return 171;
} else {
return UITableViewAutomaticDimension;
}

}

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

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

NSString *msgTypeStr = [msgTypeArr objectAtIndex:indexPath.row];
if ([msgTypeStr isEqualToString:@"msg"]) {


static NSString *simpleTableIdentifier = @"ChatConversationTableViewCell";

ChatConversationTableViewCell *cell = (ChatConversationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];



if (cell == nil)
{



NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatConversationTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];

}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.chatmsgLabel.text = [chatHistoryArr objectAtIndex:indexPath.row];
cell.timeAndDateMsgLabel.text = [timeAndDateMsgArr objectAtIndex:indexPath.row];


return cell;
}

else{
static NSString *simpleTableIdentifier = @"TicketTableViewCell";

TicketTableViewCell *cell = (TicketTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


if (cell == nil)
{


NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TicketTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];


}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.tktMsgTxtView.text = [chatHistoryArr objectAtIndex:indexPath.row];

cell.ticketDateAndTimeLbl.text =[timeAndDateMsgArr objectAtIndex:indexPath.row];



return cell;
}





}


No issues in showing data or showing different cells, only problem is overlapping. I have already tried the available solutions but no luck. Below is one of them.

adding the below code in cellForRowAtIndexPath but no use.

for(UIView *view in cell.contentView.subviews){
if ([view isKindOfClass:[UIView class]]) {
[view removeFromSuperview];
}
}


Tried a lot to fix the issue but no luck. Any help will be really appreciated.

Answer

Change your code of tableViewCell height

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *msgTypeStr = [msgTypeArr objectAtIndex:indexPath.row];
    if ([msgTypeStr isEqualToString:@"msg"]) {
        return UITableViewAutomaticDimension;
    } else {
        return 171;
    }
}

It will resolve your overlap issue.