2012-04-30 4 views
0

Cela fait un certain temps que je me bats la tête et mon dernier espoir est Stack Overflow.Relation entre les données de base, relation un-à-plusieurs

Voici l'idée de l'application. J'ai deux entités modélisées dans les données de base. L'entité Golfer a une relation un à plusieurs avec FittingSession, de sorte que chaque golfeur est capable d'avoir plus d'une session d'ajustement. Je ne suis pas en mesure d'afficher une image pour les modèles de données car je suis un nouvel utilisateur. Mais voici les détails:

ENTITÉ: GOLFEUR et essayage Attributs pour Golfeur - prenom, last_name, emailId, contactnum, image Relation: nsset * fittingSessions

Attributs pour les sessions de montage - session_number, date, emplacement, notes. Relation: Golfeur * whoPlayed

Je travaille sur un contrôleur de vue appelé ViewManager (un peu la vue de base pour toutes mes classes) et il a 2 ou 3 Custom UIView s à l'intérieur. Je les anime à chaque fois que j'en ai besoin.

Je reçois ma liste de collection Golfers dans un tableview de NSFetchedResultsController et obtenant les attributs Fitting Sessions dans une vue de table par la même technique en utilisant NSFetchedResultsController. Ma question est: Comment puis-je obtenir une séance d'essayage spécifique pour un golfeur spécifique? Que dois-je écrire dans TableViewDidSelectRow Méthode de vue parent (golfeur)? Comment puis-je gérer cette relation un à plusieurs? Voici mon code:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     if(tableView == mGolferTblView) 
     { 
      NSInteger count = [[self.fetchedResultsController sections] count]; 
      NSLog(@"count section GOLFER TABLE VIEW=%d", count); 
      return count; 
     } 

     else if(tableView == mFittingTblView) 
     { 
      return 1; 
     } 
     else { 

     } 

     return 0; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     if(tableView == mGolferTblView) 
     { 
      id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
      NSLog(@"count for array ROWS for GOLFERS =%d", [sectionInfo numberOfObjects]); 
      return [sectionInfo numberOfObjects]; 
     } 

     else if(tableView == mFittingTblView) 
     { 


mFittingSessionArray = [mFittingSessionSet allObjects]; 

    NSLog(@"array here is=%@", mFittingSessionArray); 

    NSLog(@"count for ROWS in FITTING SESSIONS table view=%d", [mFittingSessionArray count]); 
    return [mFittingSessionArray count]; 



    //  id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fittingFetchedResultsController sections] objectAtIndex:section]; 
    //  NSLog(@"count for array ROWS for FITTING SESSIONS =%d", [sectionInfo numberOfObjects]); 
    //  return [sectionInfo numberOfObjects]; 

     } 
     else { 

     } 

     return 0; 

    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     if(tableView == mGolferTblView) 
     { 
      static NSString *CellIdentifier = @"Cell"; 
      static NSInteger fullNameTag = 1; 
      static NSInteger imageTag = 2; 

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) 
      { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

       cell.selectionStyle = UITableViewCellSelectionStyleGray; 

       UILabel *fakeLbl =[[UILabel alloc] initWithFrame:CGRectMake(100, 30, 100, 30)]; 
       fakeLbl.backgroundColor = [UIColor clearColor]; 
       fakeLbl.textColor = [UIColor grayColor]; 
       [email protected]"3 days ago"; 
       [cell.contentView addSubview:fakeLbl]; 
       [fakeLbl release]; 

       UIImageView *btnImage =[[UIImageView alloc] initWithFrame:CGRectMake(250, 40, 25, 28)]; 
       btnImage.image = [UIImage imageNamed:@"badge_25x28.png"]; 
       btnImage.contentMode = UIViewContentModeScaleAspectFit; 
       [cell.contentView addSubview:btnImage]; 
       [btnImage release]; 

       UILabel *fullNameLbl =[[UILabel alloc] initWithFrame:CGRectMake(100, 0, 300, 30)]; 
       fullNameLbl.backgroundColor = [UIColor clearColor]; 
       fullNameLbl.textColor = [UIColor whiteColor]; 
       fullNameLbl.numberOfLines = 1; 
       fullNameLbl.adjustsFontSizeToFitWidth = YES; 
       fullNameLbl.tag = fullNameTag; 
       [cell.contentView addSubview:fullNameLbl]; 
       [fullNameLbl release]; 

       UIImageView *imageView =[[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 71, 91)]; 
       imageView.backgroundColor = [UIColor clearColor]; 
       imageView.tag = imageTag; 
       [cell.contentView addSubview:imageView]; 
       [imageView release]; 
      } 


      mGolfer = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

      UILabel * fullNameLbl = (UILabel *) [cell.contentView viewWithTag:fullNameTag]; 
      fullNameLbl.text = mGolfer.fullName; 

      UIImageView * imgView = (UIImageView *) [cell.contentView viewWithTag:imageTag]; 
      imgView.image = mGolfer.picture; 

      return cell; 
     } 

     if(tableView == mFittingTblView) 
     {   
      static NSString *CellIdentifier = @"Cell"; 
      static NSInteger locationTag = 1; 
      static NSInteger notesTag = 2; 

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) 
      { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

       cell.selectionStyle = UITableViewCellSelectionStyleGray; 

       UIImageView *iconImage =[[UIImageView alloc] initWithFrame:CGRectMake(10, 40, 11, 18)]; 
       iconImage.image = [UIImage imageNamed:@"icon_locationmarker_11x18.png"]; 
       iconImage.contentMode = UIViewContentModeScaleAspectFit; 
       [cell.contentView addSubview:iconImage]; 
       [iconImage release]; 

       UILabel *fakeDateLbl =[[UILabel alloc] initWithFrame:CGRectMake(180, 15, 100, 20)]; 
       fakeDateLbl.backgroundColor = [UIColor clearColor]; 
       fakeDateLbl.textColor = [UIColor grayColor]; 
       [email protected]"apr.30.2012"; 
       [cell.contentView addSubview:fakeDateLbl]; 
       [fakeDateLbl release]; 

       UIImageView *fakeImage1 =[[UIImageView alloc] initWithFrame:CGRectMake(25, 90, 43, 43)]; 
       fakeImage1.image = [UIImage imageNamed:@"icon_catDriver_43x43.png"]; 
       fakeImage1.contentMode = UIViewContentModeScaleAspectFit; 
       [cell.contentView addSubview:fakeImage1]; 
       [fakeImage1 release]; 

       UIImageView *fakeImage2 =[[UIImageView alloc] initWithFrame:CGRectMake(70, 90, 43, 43)]; 
       fakeImage2.image = [UIImage imageNamed:@"icon_catFairway_43x43.png"]; 
       fakeImage2.contentMode = UIViewContentModeScaleAspectFit; 
       [cell.contentView addSubview:fakeImage2]; 
       [fakeImage2 release]; 

       UIImageView *fakeImage3 =[[UIImageView alloc] initWithFrame:CGRectMake(115, 90, 43, 43)]; 
       fakeImage3.image = [UIImage imageNamed:@"icon_catHybrid_43x43.png"]; 
       fakeImage3.contentMode = UIViewContentModeScaleAspectFit; 
       [cell.contentView addSubview:fakeImage3]; 
       [fakeImage3 release]; 

       UIImageView *fakeImage4 =[[UIImageView alloc] initWithFrame:CGRectMake(160, 90, 43, 43)]; 
       fakeImage4.image = [UIImage imageNamed:@"icon_catIron_43x43.png"]; 
       fakeImage4.contentMode = UIViewContentModeScaleAspectFit; 
       [cell.contentView addSubview:fakeImage4]; 
       [fakeImage4 release]; 

       UIImageView *fakeImage5 =[[UIImageView alloc] initWithFrame:CGRectMake(205, 90, 43, 43)]; 
       fakeImage5.image = [UIImage imageNamed:@"icon_catWedge_43x43.png"]; 
       fakeImage5.contentMode = UIViewContentModeScaleAspectFit; 
       [cell.contentView addSubview:fakeImage5]; 
       [fakeImage5 release]; 

       UILabel *sessionLbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 7, 150, 30)]; 
       sessionLbl.backgroundColor = [UIColor clearColor]; 
       sessionLbl.textColor = [UIColor redColor]; 
       sessionLbl.text = @"session"; 
       sessionLbl.font = [UIFont boldSystemFontOfSize:18]; 
       [cell.contentView addSubview:sessionLbl]; 
       [sessionLbl release]; 

       UILabel *locationLbl =[[UILabel alloc] initWithFrame:CGRectMake(30, 32, 270, 30)]; 
       locationLbl.backgroundColor = [UIColor clearColor]; 
       locationLbl.textColor = [UIColor whiteColor]; 
       locationLbl.tag = locationTag; 
       [cell.contentView addSubview:locationLbl]; 
       [locationLbl release]; 

       UILabel *notesLbl =[[UILabel alloc] initWithFrame:CGRectMake(30, 54, 270, 30)]; 
       notesLbl.backgroundColor = [UIColor clearColor]; 
       notesLbl.textColor = [UIColor whiteColor]; 
       notesLbl.tag = notesTag; 
       [cell.contentView addSubview:notesLbl]; 
       [notesLbl release]; 


      } 

      mFittingSessionArray = [mFittingSessionSet allObjects]; 

      mFittingSession = [mFittingSessionArray objectAtIndex:indexPath.row]; 


      UILabel *locationLbl = (UILabel *)[cell.contentView viewWithTag:locationTag]; 
      locationLbl.text = mFittingSession.locationUppercase; 
      NSLog(@"location=%@", mFittingSession.locationUppercase); 

      UILabel *notesLbl = (UILabel *)[cell.contentView viewWithTag:notesTag]; 
      notesLbl.text = mFittingSession.notesInQuotes; 

      return cell; 

     } 
     return 0; 
    } 

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {  
     if(tableView == mGolferTblView) 
     { 

      mGolfer = (Golfer *)[self.fetchedResultsController objectAtIndexPath:indexPath]; 

      mGolferNameLbl.text = mGolfer.fullName; 
      mGolferHeaderPicture.image = mGolfer.picture; 

      NSSet * fittingSessionSet = mGolfer.fittingSessions; 

      mFittingSessionArray = [fittingSessionSet allObjects]; 

      NSLog(@"count for sessions=%d", [mFittingSessionArray count]); 
      NSLog(@"fiting sessions for golfer %@ are= %@", mGolfer.first_name, mFittingSessionArray); 
    } 
    } 

De plus, voici comment ajouter une nouvelle séance d'essayage à un golfeur particulier:

- (IBAction)addNewSession:(id)sender 
    { 


     AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; 

     NSManagedObjectContext * context = [applicationDelegate managedObjectContext]; 

     mFittingSession=(FittingSession*) [NSEntityDescription insertNewObjectForEntityForName:@"FittingSession" inManagedObjectContext:context]; 

     mFittingSession.location=mLocationTextField.text; 
     mFittingSession.notes=mNotesTxtView.text; 
     **mFittingSession.whoPlayed = self.golfer;** This is setting the relationship (whoPlayed is inverse relation to golfer provided by core data) 
} 

S'il vous plaît aidez-moi dans ce que je suis vraiment ne reçois pas comment faire face à la relation dans les données de base. Veuillez fournir du code ou des extraits pour que je sache ce qui se passe.

Merci

+0

Son été un jour et personne n'a répondu. Quelqu'un peut-il jeter un coup d'oeil et répondre? Ça me ferait plaisir. –

Répondre

1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if(tableView == mGolferTblView) 
    { 
     NSInteger count = [[self.fetchedResultsController sections] count]; 
     NSLog(@"count section GOLFER TABLE VIEW=%d", count); 
     return count; 
    } 

    else if(tableView == mFittingTblView) 
    { 
     NSInteger countOfSections = 1; 
     NSLog(@"count section FITTING SESSION VIEW=%d", countOfSections); 
     return countOfSections; 
    } 
    else { 

    } 

    return 0; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(tableView == mGolferTblView) 
    { 
     id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
     NSLog(@"count for ROWS in GOLFERS table view =%d", [sectionInfo numberOfObjects]); 
     return [sectionInfo numberOfObjects]; 
    } 

    else if(tableView == mFittingTblView) 
    { 
     mFittingSessionArray = [mFittingSessionSet allObjects]; 

     NSLog(@"array here is=%@", mFittingSessionArray); 

     NSLog(@"count for ROWS in FITTING SESSIONS table view=%d", [mFittingSessionArray count]); 
     return [mFittingSessionArray count]; 
    } 
    else { 

    } 

    return 0; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(tableView == mGolferTblView) 
    { 
     static NSString *CellIdentifier = @"Cell"; 
     static NSInteger fullNameTag = 1; 
     static NSInteger imageTag = 2; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

      cell.selectionStyle = UITableViewCellSelectionStyleGray; 


      UILabel *fullNameLbl =[[UILabel alloc] initWithFrame:CGRectMake(100, 0, 300, 30)]; 
      fullNameLbl.backgroundColor = [UIColor clearColor]; 
      fullNameLbl.textColor = [UIColor whiteColor]; 
      fullNameLbl.numberOfLines = 1; 
      fullNameLbl.adjustsFontSizeToFitWidth = YES; 
      fullNameLbl.tag = fullNameTag; 
      [cell.contentView addSubview:fullNameLbl]; 
      [fullNameLbl release]; 

      UIImageView *imageView =[[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 71, 91)]; 
      imageView.backgroundColor = [UIColor clearColor]; 
      imageView.tag = imageTag; 
      [cell.contentView addSubview:imageView]; 
      [imageView release]; 
     } 


     mGolfer = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

     UILabel * fullNameLbl = (UILabel *) [cell.contentView viewWithTag:fullNameTag]; 
     fullNameLbl.text = mGolfer.fullName; 

     UIImageView * imgView = (UIImageView *) [cell.contentView viewWithTag:imageTag]; 
     imgView.image = mGolfer.picture; 

     return cell; 
    } 

    else if(tableView == mFittingTblView) 
    { 
     **mFittingSessionArray = [mFittingSessionSet allObjects];** // Added this line 

     static NSString *CellIdentifier = @"Cell"; 
     static NSInteger locationTag = 1; 
     static NSInteger notesTag = 2; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

      cell.selectionStyle = UITableViewCellSelectionStyleGray; 

      UILabel *locationLbl =[[UILabel alloc] initWithFrame:CGRectMake(30, 32, 270, 30)]; 
      locationLbl.backgroundColor = [UIColor clearColor]; 
      locationLbl.textColor = [UIColor whiteColor]; 
      locationLbl.tag = locationTag; 
      [cell.contentView addSubview:locationLbl]; 
      [locationLbl release]; 

      UILabel *notesLbl =[[UILabel alloc] initWithFrame:CGRectMake(30, 54, 270, 30)]; 
      notesLbl.backgroundColor = [UIColor clearColor]; 
      notesLbl.textColor = [UIColor whiteColor]; 
      notesLbl.tag = notesTag; 
      [cell.contentView addSubview:notesLbl]; 
      [notesLbl release]; 
     } 

     mFittingSession = [mFittingSessionArray objectAtIndex:indexPath.row]; 

     UILabel *locationLbl = (UILabel *)[cell.contentView viewWithTag:locationTag]; 
     UILabel *notesLbl = (UILabel *)[cell.contentView viewWithTag:notesTag]; 

     locationLbl.text = mFittingSession.locationUppercase; 
     notesLbl.text = mFittingSession.notesInQuotes; 

     return cell; 

    } 
    else { 

    } 
    return 0; 
} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    if(tableView == mGolferTblView) 
    { 
     if(self.editing) 
     { 
      [self.golferTblView deselectRowAtIndexPath:indexPath animated:YES]; 

      [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut 

          animations:^ { 

           mEditGolfersView.hidden = NO; 
           mEditGolfersView.frame = CGRectMake(305, mEditGolfersView.frame.origin.y, mEditGolfersView.frame.size.width, mEditGolfersView.frame.size.height); 
          } 

          completion:NULL]; 

      mGolfer = (Golfer *) [self.fetchedResultsController objectAtIndexPath:indexPath]; 

      mEditFirstName.text = mGolfer.first_name; 
      mEditMiddleName.text = mGolfer.middle_name; 
      mEditLastName.text = mGolfer.last_name; 
      mEditEmailField.text = mGolfer.email_id; 
      mEditContactNum.text = mGolfer.contactNumber; 
      mEditPictureView.image = mGolfer.picture; 

      mShowDataBtn.enabled = NO; 

      return; 
     } 

     [self.golferTblView deselectRowAtIndexPath:indexPath animated:YES]; 


     [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut 

         animations:^ { 

          mGolfersView.frame = CGRectMake(-260, mGolfersView.frame.origin.y, mGolfersView.frame.size.width, mGolfersView.frame.size.height); 
         } 

         completion:^(BOOL finished){ 
          mGolfersView.hidden = YES; 
         }]; 



     [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn 

         animations:^ { 

          mFittingSessionView.hidden = NO; 
          mFittingSessionView.frame = CGRectMake(-19, mFittingSessionView.frame.origin.y, mFittingSessionView.frame.size.width, mFittingSessionView.frame.size.height); 
         } 

         completion:NULL]; 

     mGolfer = (Golfer *)[self.fetchedResultsController objectAtIndexPath:indexPath]; 

     mGolferNameLbl.text = mGolfer.fullName; 
     mGolferHeaderPicture.image = mGolfer.picture; 
     mFittingSessionSet = mGolfer.fittingSessions; 

     mFittingSessionArray = [mFittingSessionSet allObjects]; 

     NSLog(@"count for sessions=%d", [mFittingSessionArray count]); 
     NSLog(@"fiting sessions for golfer %@ are= %@", mGolfer.first_name, mFittingSessionArray); 

     **[mFittingTblView reloadData];** // Added this and this reloads the data for fitting view. 

    } 

} 
1

Peut-être que je ne comprends pas votre question, mais pour obtenir une séance d'essayage du golfeur est vraiment facile. Donc, dans le premier bloc de code, vous faites juste ce qui suit:

mGolfer = (Golfer *)[self.fetchedResultsController objectAtIndexPath:indexPath]; 
NSSet *fittingSession = mGolfer.fittingSessions; 

Vous pouvez accéder aux relations comme vous auriez accès aux autres attributs.

+0

Merci pour la réponse.Vous avez ma question à droite.Je suis en train d'essayer d'obtenir une session de montage d'un golfeur spécifique en cliquant sur cette table vue a sélectionné la ligne. Je l'ai déjà fait et voir mon code édité. Je pense que mon prédicat ne fonctionne pas correctement. Pouvez-vous regarder s'il vous plaît? –

+0

Vous ne pouvez pas interroger une relation comme vous le faites dans votre prédicat. Mais pourquoi utilisez-vous 2 FetchedResultsControllers? Je pense que vous n'avez pas besoin d'un FetchedResultController pour les sessions d'ajustement. Voici ce que je ferais: 1. Lorsque l'utilisateur sélectionne Golfeur dans la liste, obtenez les sessions de montage du golfeur comme je l'ai décrit. 2. Transformez le NSSet en NSArray et utilisez-le pour alimenter le Fitting Sessions TableView. – joern

+0

Je dois remplir ma vue Fitting Session Table avec des informations de session (emplacement, notes, etc.) Comment est-ce que je remplirai et enregistrerai l'information (données) dans mon Fitting Session TableView? Est-ce que je le fais mal? –

Questions connexes