2013-07-15 144 views
0

使用帶有索引路徑或索引集的重新加載時,會顯示_recordLabel和_dateLabel,然後不顯示,並且它會再次重複。我使用調試油漆層和其周圍地方。但是,如果使用重新加載數據一切似乎很好。我不知道爲什麼會發生這種情況。UITableView部分使用核心數據重新加載數據

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

} 

回答

0

我不知道爲什麼,這是行不通的,但是從我的經驗,我建議你使用一個框架,核心數據的工作,而不是手動做所有的工作自己,而。我個人使用Sensible TableView框架,它會自動獲取我的數據並將其顯示在表視圖中。它還處理所有實體關係,並自動爲它們創建詳細視圖。在Core Data項目上工作時節省大量的開發時間。 HTH。

+0

謝謝生病嘗試 –