UITableView使用核心数据部分重新加载数据

时间:2013-07-15 01:35:40

标签: ios core-data uitableview

当使用带索引路径或索引集的重新加载时,会显示_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];
        }
    }

}

1 个答案:

答案 0 :(得分:0)

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