UITableView Cell的内容在滚动时消失

时间:2014-03-11 20:46:24

标签: ios objective-c uitableview

我面临着消失细胞的常见问题。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WorkOut *previousWorkOut = nil;
    if (indexPath.row > 0)
    {
        previousWorkOut = [self.currentDayArray objectAtIndex:indexPath.row - 1];
    }
    static NSString *CellIdentifier = @"Cell";

    WorkOutCell *cell = nil;
    WorkOut *currentWorkOut = [self.currentDayArray objectAtIndex:indexPath.row];

    cell = [[WorkOutCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    UILabel *workoutLabel = [[UILabel alloc] initWithFrame:(CGRect){0, 0, 180, 160}];
    CGPoint workOutPoint = CGPointMake(220.0f, 25.0f);
    cell.workOutName = workoutLabel;
    cell.workOutName.center = workOutPoint;
    cell.workOutName.backgroundColor = [UIColor clearColor];
    cell.workOutName.textColor = kiOSColor;
    cell.workOutName.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:20.0f];
    cell.workOutName.text = currentWorkOut.workoutName;
    cell.workOutName.numberOfLines = 3;
    cell.workOutName.textAlignment = NSTextAlignmentLeft;
    cell.workOutName.lineBreakMode = NSLineBreakByWordWrapping;
    [cell.contentView addSubview:cell.workOutName];

    UILabel *timeLabel = [[UILabel alloc] initWithFrame:(CGRect){0, 0, 160, 160}];
    CGPoint timePoint = CGPointMake(40.0f, 25.0f);
    cell.timeLabel = timeLabel;
    cell.timeLabel.center = timePoint;
    cell.timeLabel.backgroundColor = [UIColor clearColor];
    cell.timeLabel.textColor = kIOSRedColor;
    cell.timeLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:45.0f];
    if (![currentWorkOut.workoutTime isEqualToString:previousWorkOut.workoutTime])
    {
        cell.timeLabel.text = currentWorkOut.workoutTime;
    }
    cell.timeLabel.textAlignment = NSTextAlignmentLeft;
    cell.timeLabel.transform = CGAffineTransformScale(cell.timeLabel.transform, 0.4f, 0.4f);
    [cell.contentView addSubview:cell.timeLabel];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = [UIColor whiteColor];

    if (![currentWorkOut.workoutTime isEqualToString:previousWorkOut.workoutTime])
    {
        UIView *upperLine = [[UIView alloc] initWithFrame:(CGRect){50, 2, 320, 1}];
        upperLine.backgroundColor = kiOSLightColor;
        [cell.contentView addSubview:upperLine];
    }
    else
    {
        UIView *upperLine = [[UIView alloc] initWithFrame:(CGRect){60, 2, 320, 1}];
        upperLine.backgroundColor = kiOSLightColor;
        [cell.contentView addSubview:upperLine];
}

    UIView *separatorLine = [[UIView alloc] initWithFrame:(CGRect){55, 80, 320, 1}];
    cell.upperLine = separatorLine;
    cell.upperLine.backgroundColor = [UIColor lightGrayColor];
    cell.upperLine.layer.opacity = 0.0f;
    [cell.contentView addSubview:cell.upperLine];
    NSString *w = currentWorkOut.workoutDescription;
     NSDictionary *d = [NSDictionary dictionaryWithObject:[UIFont fontWithName:@"HelveticaNeue-Thin" size:16.0f] forKey: NSFontAttributeName];
    CGSize maximumLabelSize = CGSizeMake(255, 295);
    CGSize expectedLabelSize = [w boundingRectWithSize:maximumLabelSize
                                                     options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
                                                  attributes:d context:nil].size;

    UILabel *workoutDescription = [[UILabel alloc] initWithFrame:(CGRect){50, 85, 255, expectedLabelSize.height}];
    cell.workOutDescription = workoutDescription;
    cell.workOutDescription.lineBreakMode = NSLineBreakByWordWrapping;
    cell.workOutDescription.numberOfLines = 0;
    cell.workOutDescription.text = [currentWorkOut.workoutDescription stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    cell.workOutDescription.textAlignment = NSTextAlignmentLeft;
    cell.workOutDescription.textColor = [UIColor darkGrayColor];
    cell.workOutDescription.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:16.0f];
    cell.workOutDescription.layer.opacity = 0.0f;
    [cell.contentView addSubview:cell.workOutDescription];
    return cell;
}

更新单元格的方法,我改变了单元格的参数:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isOpen == YES && [self.selectedIndexPath isEqual:indexPath])
    {
        WorkOutCell *cell = (WorkOutCell*)[tableView cellForRowAtIndexPath:indexPath];
        [UIView animateWithDuration:0.2f delay:0.0f options:UIViewAnimationOptionCurveLinear animations:^
         {
             cell.timeLabel.transform = CGAffineTransformScale(cell.timeLabel.transform, 0.4f, 0.4f);
             CGPoint timePoint = CGPointMake(40.0f, 25.0f);
             cell.timeLabel.center = timePoint;

             CGPoint workoutPoint = CGPointMake(220.0f, 25.0f);
             cell.workOutName.center = workoutPoint;
             [UIView animateWithDuration:0.4f delay:0.7f options:UIViewAnimationOptionCurveLinear animations:^
              {
                  cell.workOutDescription.layer.opacity = 0.0f;
                  cell.upperLine.layer.opacity = 0.0f;
              }
                              completion:^ (BOOL finished)
              {
              }];
         }
                         completion:^ (BOOL finished)
         {
             self.isOpen = NO;
             [self.table beginUpdates];
             [self.table endUpdates];
         }];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    //
    //...
    self.selectedIndexPath = indexPath;
}

我已经尝试dequeueReusableCellWithIdentifier,但它对我不起作用。

更新了以下代码:

  • 创建单元格
  • 更新单元格状态,查看

0 个答案:

没有答案