【问题标题】:UITableView mess up after resizing | Objective-C调整大小后 UITableView 混乱 | Objective-C
【发布时间】:2017-10-11 00:20:44
【问题描述】:

我正在使用 TableView 创建聊天应用程序。每个单元格代表一个味精。单元格高度根据 msg 的内容而变化。 例如,如果它是一张照片,它将有一个固定的尺寸。 如果它是一个文本,它的大小将取决于 msg 中的行数。 当用户尝试根据键盘的大小发送消息时,整个 TableView 的大小也会发生变化。

问题是,当 tableView 大小发生变化时,tableView 单元格会变得混乱! 这只发生在向下滚动时(重新渲染消失的单元格)

更新:如果某些单元格不可见(大 tableView),也会发生这种情况。在重新调整大小之前。而且它也没有任何类型。因为如果消息都是文本,它会做同样的事情

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

【问题讨论】:

    标签: ios objective-c iphone uitableview


    【解决方案1】:

    我找到了答案here

    问题出在小区标识符上

    代码之前:

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

    代码后:

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

    【讨论】:

      【解决方案2】:

      1 - 当键盘 Appers 获取键盘大小时 -

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

      2-如果您的 chatTable 包含任何行 -

      whwre [messageList] 是行数

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-24
        • 1970-01-01
        • 2012-05-07
        • 1970-01-01
        • 2013-11-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多