2014-04-22 5 views
1

저는 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 방법 :

내가 messages.Here를 표시하기위한 사용자 정의 셀을 사용 (간체) 내 코드입니다

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

그러나 새로 생성 된 세포가 오는 이유를 알아낼 수 없습니다 이전 셀 내용을 사용하고 다시 그릴 때 자신의 콘텐츠를 바꿉니다 (예 : 위아래로 스크롤 할 때)

+0

실제 코드인가요? 'configureCell : ...'메소드에서'rightMessageCell'이란 무엇입니까? –

+0

@MartinR 메시지 셀의 하위 클래스로, 텍스트를위한 콘센트 보유 messageTextView – user2786037

+0

'RightMessageCell' 클래스가 아니라'rightMessageCell' 변수를 의미합니다. 그게'cell.messageTextView.text = ...'가 아니겠습니까? –

답변

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을 다시 사용하기 전에 호출되므로 콘텐츠/레이아웃을 기본값으로 재설정 할 수 있습니다.

내가 문제가 있음을보고 또 다른 영역은 여기 rightMessageCellcell입니다 :

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

코드는도 위의 방법으로 컴파일 할 수 없게한다.

+0

이것은 셀의 내용을 잘못 표시하거나 셀 내용을 비동기 적으로 채우는 데 문제가 있습니다. 이 경우 문제가 장기적이며 셀을 새로 고칠 때만 해결되므로 많은 영향을 미치지 않습니다. –

+0

나는 그 스 니펫을 믿는다. 그는 그의 셀에 빈 문자열 만 가질 것이다. 문제는 셀을 다시 사용한 후에 텍스트가 제대로 설정되지 않는다는 것입니다. – streem

+0

나는 이미 그것을하려했지만 텍스트가 cellForIndexPath 메소드에 의해 덮어 쓰여지지 않는 이유를 이해할 수 없다. 또 다른 문제는 레이아웃을 재설정하는 방법입니다. cellForIndexPath 메서드에서 같은 콘센트에 텍스트를 설정하고 레이아웃을 다시 설정하는 방법을 보여 주더라도 텍스트를 nil로 설정해야하는 이유에 대해 자세히 설명해 주시겠습니까? – user2786037

관련 문제