2016-11-18 106 views
1

我有一个UITableView与两个不同的自定义单元格。一个会显示正常的文字信息,另一个会显示一些文字和图像。我能够使用两种不同的自定义细胞cellForRowAtIndexPath并能看到两个不同的细胞像下面 enter image description hereUITableview有两个不同的自定义单元格重叠,而滚动

但每当我滚动tableview中然后将细胞重叠像下面。

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

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

回答

1

更改tableViewCell高度的代码

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

它能够解决您的问题重叠。

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]; 
} 
+0

这不适合我。我正在使用自定义单元格。感谢您的回复 – Madhu