【问题标题】:UITableview with two different custom cells overlapping while scrollingUITableview 滚动时有两个不同的自定义单元格重叠
【发布时间】:2016-11-18 09:49:55
【问题描述】:

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

但是每当我滚动表格视图时,单元格就会像下面那样重叠。

下面是我的代码

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

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

【问题讨论】:

    标签: ios objective-c iphone xcode uitableview


    【解决方案1】:

    更改tableViewCell高度的代码

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

    它将解决您的重叠问题。

    【讨论】:

      【解决方案2】:

      试试这个

      NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row];
      
      UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
      
      if (cell == nil){
          cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      }
      

      【讨论】:

      • 这对我不起作用。我正在使用自定义单元格。感谢回复
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多