0

一段时间以来,我一直在努力解决这个问题,我最后的希望是 Stack Overflow。

这是应用程序的想法。我有两个在核心数据中建模的实体。该Golfer实体与 具有一对多的关系FittingSession,因此每个高尔夫球手都能够进行一次以上的试穿。由于我是新用户,因此无法发布数据模型的图像。但这里有详细信息:

实体:高尔夫球手的 GOLFER 和 FITTING SESSION 属性 - first_name、last_name、emailId、contactNum、picture 关系:NSSet * FittingSessions

拟合会话的属性 - session_number、日期、位置、注释。关系: Golfer * whoPlayed

我正在开发一个名为 ViewManager 的视图控制器(我所有类的基本视图),它UIView里面有 2-3 个 Custom 。每当我需要它们时,我都会为它们设置动画。

我从表格视图中获取我的 Golfers 列表集合,NSFetchedResultsController并通过使用NSFetchedResultsController. 我的问题是:我如何为特定的高尔夫球手获得特定的试衣时间?我必须在 Parent( Golfer )View 的 TableViewDidSelectRow 方法中写什么?我该如何处理这种一对多的关系?这是我的代码:

     - (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];
                fakeLbl.text=@"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];
                fakeDateLbl.text=@"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);
    }
    }

此外,这也是我为特定高尔夫球手添加新试穿会话的方式:

  - (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)
}

请帮助我,因为我真的不知道如何处理核心数据中的关系。请提供一些代码或片段,以便我知道发生了什么。

谢谢

4

2 回答 2

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.

    }

}
于 2012-05-02T00:44:20.243 回答
1

也许我没有正确理解您的问题,但是参加 Golfer's Fitting Session 真的很容易。因此,在第一个代码块中,您只需执行以下操作:

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

您可以像访问其他属性一样访问关系。

于 2012-05-01T17:31:19.223 回答