Ios 调整大小后的UITableView混乱|目标-C
我正在使用TableView创建聊天应用程序。其中每个单元格表示一个消息。单元格高度根据msg的内容而变化。 例如,如果它是一张照片,它将有一个固定的大小。 如果它是一个文本,它的大小取决于msg中的行数。 此外,当用户尝试根据键盘大小发送消息时,整个TableView的大小也会发生变化 问题是,当tableView大小改变时,tableView单元格会变得混乱! 这仅在向下滚动时发生(重新渲染消失的单元格) 更新:如果某些单元格不可见(大tableView),也会发生这种情况。在重新调整尺寸之前。而且它也没有任何与类型相关的内容。因为如果MSG都是文本,它也会这样做Ios 调整大小后的UITableView混乱|目标-C,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我正在使用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)];
}
我找到了答案
问题在于单元标识符
代码之前:
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];
}
}
1-当键盘apper获得键盘大小时-
keyboardInfo = [notification userInfo];
kbSize = [[keyboardInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
2-如果您的聊天表包含任何行-
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];
}
}