UITableView显示错误的单元格内容

时间:2014-04-22 15:20:48

标签: ios objective-c uitableview

我已经坚持使用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];
}

但是我无法弄清楚为什么新创建的单元格会带有以前的单元格内容并在重新绘制时替换它们自己的内容(例如,当我向上和向下滚动时)

1 个答案:

答案 0 :(得分:2)

UITableViewCell子类中,您需要实现此方法:

-(void)prepareForReuse {
    [super prepareForResuse];
    [self.textLabel setText:nil];
    // reset layout
    // reset text on your labels
    // reset other properties such as images or text colour
}

在UITableViewCell即将重复使用之前调用它,这样您就有机会将内容/布局重置为默认值。

我发现存在问题的另一个方面是cell vs rightMessageCell

- (void) configureCell:(RightMessageCell *) cell forIndexPath:(NSIndexPath *) indexPath {
    ChatMessageModel *message = [self.fetchController objectAtIndexPath:indexPath]; 
    rightMessageCell.messageTextView.text = message.messageText;
}

您的代码甚至无法使用上述方法进行编译。