UITableview,滚动时重叠两个不同的自定义单元格

时间:2016-11-18 09:49:55

标签: ios objective-c iphone xcode uitableview

我有一个UITableView,有两个不同的自定义单元格。一个将显示正常的文本消息,另一个将显示一些文本和图像。我可以在cellForRowAtIndexPath中使用两个不同的自定义单元格,并且能够看到两个不同的单元格,如下所示 enter image description here

但每当我滚动桌面视图时,单元格就像下面一样重叠。

enter image description here

以下是我的代码

-(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;
    }





}

显示数据或显示不同的单元格没有问题,只有问题重叠。我已经尝试了可用的解决方案,但没有运气。以下是其中之一。

在cellForRowAtIndexPath中添加以下代码但不使用。

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

尝试了很多来解决这个问题,但没有运气。任何帮助都将非常感激。

2 个答案:

答案 0 :(得分:1)

更改tableViewCell高度的代码

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

它将解决您的重叠问题。

答案 1 :(得分:0)

尝试一次

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row];

UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];

if (cell == nil){
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}