来自另一个UITableViewCell的数据在当前单元格中重复

时间:2012-08-20 00:29:58

标签: iphone ios uitableview

当重新使用我的自定义UITableViewCell时,不会为当前单元格重置所有数据。为特定的UILabel插入来自重用单元的数据。

我有菜肴的桌面视图。菜肴有评论。我想在tableviewcell中显示每个菜的评论。根据有多少评论,我只在每个单元格中制作那么多评论标签。然而,当一个细胞被重复使用时,会出现错误的评论 - 来自不同菜单的评论。

我正在使用CoreData,而Dish有很多评论,我们只想说我只想显示前两条评论。

我在IB中创建了两个UILabel,高度和宽度为零,以便调整每个注释的长度。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
    DishCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DishCell"];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{ ...I put all of the Dishes in a NSFetchedResultsController instance so *dish is a NSManagedObject that has many comments

      NSSet *commentsSet = dish.comments;
      NSSortDescriptor *sortByDate = [[NSSortDescriptor alloc]initWithKey:@"date"  ascending:YES];
      NSArray *commentArray = [commentsSet sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortByDate]];

     if (commentsSet.count< 1) {
        dishCell.comment1label.text = @"";
        dishCell.comment2label.text = @"";
     }else {

    Comment *comment = [commentArray objectAtIndex:0];

    NSString *commenterName = comment.displayName;
    NSString *commentString = comment.commentText;
    NSString *text = [NSString stringWithFormat:@"%@: %@", commenterName, commentString];

    CGSize comment1Size = [text sizeWithFont:[UIFont systemFontOfSize:10.0f] constrainedToSize:CGSizeMake(constraint.width - dishCell.commentBubble.frame.size.width + 4, constraint.height) lineBreakMode:UILineBreakModeWordWrap];

    //Ignore this setFrame - it works
    [dishCell.comment1label setFrame:CGRectMake(32, faveIconYpos - 2 + dishCell.favIcon.frame.size.height, 298 - dishCell.commentBubble.frame.size.width, comment1Size.height + 2)];

    dishCell.text = text;

    if (commentsSet.count > 1) {
        Comment *comment2 = [commentArray objectAtIndex:1];
        [dishCell.comment2label setTextColor:[UIColor whiteColor]];
        [dishCell.comment2label setFont:[UIFont systemFontOfSize:12.0]];

        NSString *text2 = [NSString stringWithFormat:@"%@: %@", comment2.displayName, comment2.commentText];

        CGSize comment2Size = [text2 sizeWithFont:[UIFont systemFontOfSize:10.0f] constrainedToSize:CGSizeMake(constraint.width - dishCell.commentBubble.frame.size.width + 4, constraint.height) lineBreakMode:UILineBreakModeWordWrap];
        [dishCell.comment2label setFrame:CGRectMake(32, faveIconYpos + dishCell.favIcon.frame.size.height + dishCell.testcom.frame.size.height - 2, 298 - dishCell.commentBubble.frame.size.width, comment2Size.height + 2)];
        dishCell.comment2label.text = text2; 
    }

我已经解析了一些代码,但主要的是来自另一个单元格的数据被插入到我的标签中。为什么会这样?

0 个答案:

没有答案