繁体   English   中英

UITableView显示错误的单元格内容

[英]UITableView shows wrong cell contents

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

但是我不知道为什么新创建的单元格具有以前的单元格内容,并在重绘它们时替换它们自己的内容(例如,当我上下滚动时)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM