核心数据与同一UItableView中的关系

时间:2012-03-19 14:04:22

标签: uitableview core-data relationship

我需要将与“Child Entity”一对多关系的“Main Entity”加载到同一个UITableView中,我该怎么办? 提前谢谢。

更新

我使用以下代码填充UITableView:

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{    
    return [[[fetchedResultsController sections] objectAtIndex:section] name];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];

  if (self.selectedIndexPath) {

    return [sectionInfo numberOfObjects] + [self.childArray count];

  }else {
    return [sectionInfo numberOfObjects];
  } 
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    OreRecupero *selectedObj=(OreRecupero*)[self.fetchedResultsController objectAtIndexPath:indexPath];

OrarioMainCell *cell = nil;

if (indexPath == self.selectedIndexPath) {
    NSUInteger giorniCount = [selectedObj.giorni count];
    NSInteger row = indexPath.row;

    if (indexPath.row < giorniCount) {

        cell = (OrarioMainCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];

        if (cell == nil) {
            cell=(OrarioMainCell *)[[[NSBundle mainBundle]loadNibNamed:@"myCustomCell" owner:self options:nil]objectAtIndex:11];
            radiusLabel=[[LabelRadiusAndText alloc]init];
        }

        UIImageView *bgImage=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fondoCella"]]autorelease];
        cell.backgroundView=bgImage;

        [df setDateFormat:@"HH:mm"];

        cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@",[df stringFromDate:selectedObj.oraInizio],[df stringFromDate:selectedObj.oraFine]];

    } else {

        cell = (OrarioMainCell *)[tableView dequeueReusableCellWithIdentifier:@"DetailCell"];
        if (cell == nil) {
            cell=(OrarioMainCell *)[[[NSBundle mainBundle]loadNibNamed:@"myCustomCell" owner:self options:nil]objectAtIndex:13];
            radiusLabel=[[LabelRadiusAndText alloc]init];
        }    

        GiornoRecupero *giorno = [childArray objectAtIndex:row];
        cell.textLabel.text = [NSString stringWithFormat:@"%i", giorno.oreStraordinario];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%i", giorno.durata];
    }
} else {

    cell = (OrarioMainCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if (cell == nil) {
        cell=(OrarioMainCell *)[[[NSBundle mainBundle]loadNibNamed:@"myCustomCell" owner:self options:nil]objectAtIndex:11];
        radiusLabel=[[LabelRadiusAndText alloc]init];
    }

    UIImageView *bgImage=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fondoCella"]]autorelease];
    cell.backgroundView=bgImage;


    [df setDateFormat:@"HH:mm"];

    cell.orariLbl.text = [NSString stringWithFormat:@"%@ - %@",[df stringFromDate:selectedObj.oraInizio],[df stringFromDate:selectedObj.oraFine]];

}

   return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OreRecupero *selectedObj=(OreRecupero*)[self.fetchedResultsController objectAtIndexPath:indexPath];

NSIndexPath *dropDownCellIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1
                                                        inSection:indexPath.section];
//if user tapped the same row twice let's start getting rid of the control cell
if (!self.selectedIndexPath) {
    // Show Drop Down Cell        
    for (GiornoRecupero *giorno in selectedObj.giorni) {

        [self.childArray addObject:giorno];

    }

    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath] 
                     withRowAnimation:UITableViewRowAnimationTop];        

    self.selectedIndexPath = indexPath;    
} else {
    NSIndexPath *currentdropDownCellIndexPath = [NSIndexPath indexPathForRow:self.selectedIndexPath.row + 1
                                                                   inSection:self.selectedIndexPath.section];

    if (indexPath.row == self.selectedIndexPath.row) {
        // Hide Drop Down Cell
        [self.childArray removeAllObjects];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath]
                         withRowAnimation:UITableViewRowAnimationTop];

        self.selectedIndexPath = nil;
    } else if (indexPath.row == currentdropDownCellIndexPath.row) {
        // Dropdown Cell Selected - No Action
        return;
    } else {
        // Switch Dropdown Cell Location     
        [self.myTableView beginUpdates];
        // Hide Dropdown Cell

        [self.childArray removeAllObjects];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath]
                         withRowAnimation:UITableViewRowAnimationAutomatic];

        // Show Dropdown Cell            
        NSInteger dropDownCellRow = indexPath.row + ((indexPath.row >= currentdropDownCellIndexPath.row) ? 0 : 1);
        dropDownCellIndexPath = [NSIndexPath indexPathForRow:dropDownCellRow
                                                   inSection:indexPath.section];

        for (GiornoRecupero *giorno in selectedObj.giorni) {

            [self.childArray addObject:giorno];

        }

        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath] 
                         withRowAnimation:UITableViewRowAnimationAutomatic];        

        self.selectedIndexPath = [NSIndexPath indexPathForRow:dropDownCellIndexPath.row - 1
                                                    inSection:dropDownCellIndexPath.section];

        [self.myTableView endUpdates];

    }        
}     
}

我想使用didSelectRowAtIndexPath方法插入新行,从“Child Entity”获取数据

0 个答案:

没有答案