0

当使用索引路径或索引集重新加载时,_recordLabel 和 _dateLabel 会显示,然后不显示,并且会再次重复。我正在使用调试油漆层,但它无处不在。但如果使用重新加载数据一切似乎都很好。我不知道为什么会这样。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier;
    cellIdentifier = [_sections objectAtIndex:indexPath.section];


    XFitGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[XFitGroupCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [self stampCell:cell atIndexPath:indexPath];
    }

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

- (void)stampCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{

    if ([cell.reuseIdentifier isEqualToString:@"exerciseDescriptionCell"]) {

        if (!_textView) {
            _textView = [[UITextView alloc] initWithFrame:CGRectMake(0.f, 22.f, cell.frame.size.width, 66.f)];
        }
        _textView.editable = NO;
        _textView.font = [UIFont fontWithName:@"Avenir-Light" size:14.f];
        [cell.contentView addSubview:_textView];

    }
    else if ([cell.reuseIdentifier isEqualToString:@"exerciseResultsCell"]) {

        UILabel *recordLabelTag = [[UILabel alloc] initWithFrame:CGRectMake(10.f, 28.f, 55.f, 14.f)];

        if ([_exercise.eType.name isEqualToString:@"Strength"]) {
            recordLabelTag.text = @"PR";
        }
        else {
            recordLabelTag.text = @"UB";
        }

        recordLabelTag.font = [UIFont fontWithName:@"Avenir-Heavy" size:14.f];
        recordLabelTag.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:recordLabelTag];

        if (!_recordLabel) {
            _recordLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.f, 48.f, 55.f, 12.f)];
        }
        _recordLabel.font = [UIFont fontWithName:@"Avenir-Light" size:12.f];
        _recordLabel.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:_recordLabel];

        UILabel *dateLabelTag = [[UILabel alloc] initWithFrame:CGRectMake(70.f, 28.f, 55.f, 14.f)];
        dateLabelTag.text = @"Date";
        dateLabelTag.font = [UIFont fontWithName:@"Avenir-Heavy" size:14.f];
        dateLabelTag.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:dateLabelTag];

        if (!_dateLabel) {
            _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(70.f, 48.f, 55.f, 12.f)];
        }
        _dateLabel.font = [UIFont fontWithName:@"Avenir-Light" size:12.f];
        _dateLabel.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:_dateLabel];

        UIButton *recordButton = [[UIButton alloc] initWithFrame:CGRectMake(cell.frame.size.width - 73.f, 30.f, 65.f, 28.f)];
        recordButton.backgroundColor = [UIColor colorWithRed:77.f/255.f green:123.f/255.f blue:209.f/255.f alpha:1.f];
        recordButton.layer.cornerRadius = 4.f;
        [recordButton addTarget:self action:@selector(addRecord) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:recordButton];

        UILabel *newPRLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.f, 7.f, 55.f, 14.f)];

        if ([_exercise.eType.name isEqualToString:@"Strength"]) {
            newPRLabel.text = @"New PR";
        }
        else {
            newPRLabel.text = @"New UB";
        }

        newPRLabel.textColor = [UIColor whiteColor];
        newPRLabel.font = [UIFont fontWithName:@"Avenir-Book" size:14.f];
        [recordButton addSubview:newPRLabel];

        UIButton *showAllButton = [[UIButton alloc] initWithFrame:CGRectMake(cell.frame.size.width - 73.f - 75.f, 30.f, 65.f, 28.f)];
        showAllButton.backgroundColor = [UIColor colorWithRed:98.f/255.f green:233.f/255.f blue:126.f/255.f alpha:1.f];
        showAllButton.layer.cornerRadius = 4.f;
        [cell.contentView addSubview:showAllButton];

        UILabel *showAllLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.f, 7.f, 55.f, 14.f)];
        showAllLabel.text = @"Show All";
        showAllLabel.textColor = [UIColor whiteColor];
        showAllLabel.font = [UIFont fontWithName:@"Avenir-Book" size:14.f];
        [showAllButton addSubview:showAllLabel];

    }
}


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    if ([cell.reuseIdentifier isEqualToString:@"exerciseDescriptionCell"]) {
        [(XFitGroupCell *)cell setText:@"Description"];
        [(XFitGroupCell *)cell setImage:[UIImage imageNamed:@"descriptionIcon"]];

        _textView.text = _exercise.overview;

    }
    else if ([cell.reuseIdentifier isEqualToString:@"exerciseResultsCell"]) {
        [(XFitGroupCell *)cell setText:@"Previous Results"];
        [(XFitGroupCell *)cell setImage:[UIImage imageNamed:@"recordIcon"]];

        ExerciseRecord *personalRecord = _exercise.personalRecord;

        NSLog(@"Received: %@\n", personalRecord.score);
        _recordLabel.text = [personalRecord.score stringValue];
        NSLog(@"Text : %@\n", _recordLabel.text);
        _dateLabel.text = [_dateFormatter stringFromDate:personalRecord.date];
    }
}


#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 88.f;
    }
    else if (indexPath.section == 1) {
        return 66.f;
    }
    return 0.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 7.f;
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1) {
        if (buttonIndex == 1) {
            ExerciseRecord *eRecord = [NSEntityDescription insertNewObjectForEntityForName:@"ExerciseRecord" inManagedObjectContext:_managedObjectContext];

            eRecord.score = [NSNumber numberWithInt:[[alertView textFieldAtIndex:0].text intValue]];
            eRecord.date = [NSDate date];
            eRecord.exercise = _exercise;

            NSError *error;
            if (![_managedObjectContext save:&error]) {
                NSLog(@"%@ %@", error, [error userInfo]);
                abort();
            }



    [_tableView reloadData];
//[_tableView beginUpdates];
                //[_tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
                //[_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
//[_tableView endUpdates];
        }
    }

}
4

1 回答 1

0

我不确定为什么这不起作用,但根据我的经验,我建议您在使用 Core Data 时使用框架,而不是自己手动完成所有工作。我个人使用 Sensible TableView 框架,它会自动获取我的数据并将其显示在表格视图中。它还处理所有实体关系并自动为它们创建详细视图。在处理 Core Data 项目时为我节省了大量的开发时间。HTH。

于 2013-07-15T19:55:23.730 回答