- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* CellIdentifier = @"MessageCellIdentifier";
MessageTableViewCell* cell = (MessageTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[MessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Message* message = [self.dataModel.messages objectAtIndex:indexPath.row];
if ([message.imgDisplay isEqualToString:@"image"]) {
[cell setMessage:message];
}
else
{
[cell setMessage:message];
}
return cell;
}
- (void)setMessage:(Message*)message {
CGPoint point = CGPointZero;
NSString* senderName;
BubbleType bubbleType;
if ([message isSentByUser])
{
bubbleType = BubbleTypeRighthand;
senderName = NSLocalizedString(@"You", nil);
point.x = self.bounds.size.width - message.bubbleSize.width;
label.textAlignment = UITextAlignmentRight;
}
else
{
bubbleType = BubbleTypeLefthand;
senderName = message.senderName;
label.textAlignment = UITextAlignmentLeft;
}
// Resize the bubble view and tell it to display the message text
CGRect rect;
rect.origin = point;
rect.size = message.bubbleSize;
bubbleView.frame = rect;
if ([message.imgDisplay isEqualToString:@"image"]) {
[bubbleView setImage:[NSString stringWithFormat:@"%@.jpg",message.chatimageName] bubbleType:bubbleType];
}
else
{
[bubbleView setText:message.text bubbleType:bubbleType];
}
// Format the message date
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDoesRelativeDateFormatting:YES];
NSString* dateString = message.date;
// Set the sender's name and date on the label
label.text = [NSString stringWithFormat:@"%@ @ %@", senderName, dateString];
[label sizeToFit];
label.frame = CGRectMake(8, message.bubbleSize.height, self.contentView.bounds.size.width - 16, 16);
}
- (int)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataModel.messages count];
}
我正在开发一个聊天应用程序,下图显示了 chatViewController 的视图。它工作正常。有时我收到异常为 NSInternalInconsistencyException',原因:'无效更新:第 0 节中的行数无效。更新后的现有节 (1) 必须等于更新前该节中包含的行数 (1),加上或减去从该节插入或删除的行数(1 插入,0 删除),加上或减去移入或移出该部分的行数(0移入,0移出)。'。我不知道是什么导致了问题。请有人帮忙解决这个问题。