【发布时间】:2014-04-23 05:51:16
【问题描述】:
我一直坚持使用 UITableView 并添加单元格。
我有基于 UITableView 的聊天应用程序来显示消息。当用户使用旧消息打开聊天时,一切正常。但是当用户添加新消息时 - 该消息的单元格填充了错误的内容。
我使用自定义单元格来显示消息。这是我的代码(简化):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Im using storyboard, so I don't need if (!cell) part
RightMessageCell *cell = (RightMessageCell *) [self.messageTableView dequeueReusableCellWithIdentifier:@"right_text_message"];
[self configureCell:cell forIndexPath:indexPath];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
[self configureCell:self.offscreenCell forIndexPath:indexPath];
[self.offscreenCell setNeedsLayout];
[self.offscreenCell layoutIfNeeded];
result = [self.offscreenCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
}
- (void) configureCell:(RightMessageCell *) cell forIndexPath:(NSIndexPath *) indexPath {
ChatMessageModel *message = [self.fetchController objectAtIndexPath:indexPath];
rightMessageCell.messageTextView.text = message.messageText;
}
和FRC方法:
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.messageTableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.messageTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
case NSFetchedResultsChangeDelete:
[self.messageTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
case NSFetchedResultsChangeUpdate:
[self.messageTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.messageTableView endUpdates];
}
但我不明白为什么新创建的单元格会带有以前的单元格内容并在重绘时替换它们自己的内容(例如,当我上下滚动时)
【问题讨论】:
-
那是你的实际代码吗?
configureCell:...方法中的rightMessageCell是什么? -
@MartinR 它是消息单元的子类,为文本 messageTextView 提供出口
-
我的意思是
rightMessageCell变量,而不是RightMessageCell类。不应该是cell.messageTextView.text = ...吗? -
@user2786037 我看到的和 Martin 一样。您将要配置的单元格传递给 configureCell,然后通过将消息文本设置到其他单元格 (rightMessageCell) 来忽略它。我的猜测是你需要使用 cell.messageTextView.text = ... 而不是 rightMessageCell...
-
@MartinR 哦,对不起,这是一个问题,而不是代码中的错误..
标签: ios objective-c uitableview