Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Fabio Floris
Fabio Floris
526 Points

PFRelation create with UiButton on Cell

Hello everyone ... I'm trying to create PFRelation using a button in a TableView with custom cell, but I can not create these relationships, I only creates a relationship with a specific user regardless of the cell that I select. ..

Where am I doing wrong?

       - (IBAction)FFAddAmiciAction:(id)sender {
             NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
            PFObject *searchedUser = [self.objects objectAtIndex:indexPath.row];
            PFUser *user = [PFUser currentUser  ];
             PFRelation *relation = [user relationforKey:@"Amic"];
               [relation addObject:searchedUser];
                 [user saveInBackground];

}

1 Answer

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Set breakpoints to see if a row is being selected and which user you are getting back in searchedUser variable

Fabio Floris
Fabio Floris
526 Points

Amit Hello and thanks for the reply ... In the end I managed to find the problem for the button ... was enough to set the action as [sender tag] and everything worked ...

I was following the tutorial on your friends list and show what friends are and what they are not friends through the checkmark ... Surely it is not my fault but I can not get it to work ...

In my tableviewController I also implemented the Search Bar could be the problem?? Could you help me understand with a practical example ... are two days that I try but I just can not understand how I can implement your tutorial within my viewcontroller with searchbar

import "FFPrincipal.h"

            #import "FFCellFindUser.h"

            @interface FFPrincipal ()


            @end

            @implementation FFPrincipal
            @synthesize FFSearchBar, filteredObjects,allObjects,isFiltered,icon;

            - (void)viewDidLoad
            {
            [super viewDidLoad];


            self.Relazioni = [[PFUser currentUser] objectForKey:@"Friend"];

            PFQuery *query = [self.Relazioni query];
            [query orderByAscending:@"username"];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (error) {
                NSLog(@"errore");
            } else {
                self.amici = objects;
                [self.FFTableViewFindUser reloadData];
            }
            }];


            UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
            [overlayView setBackgroundColor:[UIColor colorWithRed:(40/255.0) green:(40/255.0) blue:(40/255.0) alpha:(1)]];
            [FFSearchBar addSubview:overlayView]; // navBar is your UINavigationBar instance
            self.FFTableViewFindUser.delegate=self;
            self.FFTableViewFindUser.dataSource =self;
            self.FFSearchBar.delegate = self;
            [self retrieveFromParse];
            UITapGestureRecognizer *FFResignKeyboard = [[UITapGestureRecognizer alloc]
                                                    initWithTarget:self
                                                    action:@selector(FFDeleteKeyboardOnView)];

            [self.view addGestureRecognizer:FFResignKeyboard];



            PFUser *CurrentUser = [PFUser currentUser];
            if (CurrentUser) {
            NSLog(@"Connesso con:%@", CurrentUser.username); }

            else {
            [self performSegueWithIdentifier:@"Login" sender:self]; }
            }

            -(void)viewDidAppear:(BOOL)animated {
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
            [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
            [self ShowBadgeNumber];

            }




            -(void)ShowBadgeNumber {
            PFQuery *QueryForBadgeNumberFriend = [PFQuery queryWithClassName:FF_USER_CLASS];
            [QueryForBadgeNumberFriend whereKey:@"conto" equalTo:[PFUser currentUser]];
            [QueryForBadgeNumberFriend countObjectsInBackgroundWithBlock:^(int number, NSError *error) {

            NSNumber *ValoreMediaPonderata = [[PFUser currentUser] objectForKey:@"conto"];
            number = [ValoreMediaPonderata intValue];
            if (number <= 0) {
            [icon setHidden:YES];

            } else {
            [icon setHidden:NO];
                [icon.layer setMasksToBounds:YES];
                [icon.layer setCornerRadius:10.5f];
                [icon setText:[NSString stringWithFormat:@"%d", number]];
            } }];

            }

            - (void)FFDeleteKeyboardOnView {
            [self.FFSearchBar resignFirstResponder];
            }



            - (void) retrieveFromParse {


            PFQuery *retrievePets = [PFQuery queryWithClassName:FF_USER_CLASS];
            [retrievePets orderByAscending:FF_USER_NOMECOGNOME];
            [retrievePets findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error) {
                NSLog(@"%@", objects);
                allObjects = [[NSMutableArray alloc] init];
                for (PFObject *object in objects) {
                    [allObjects addObject:object];

                }

            }
            [self.FFTableViewFindUser reloadData];
            }];
            }



            -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
            return 1;
            }

            - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
            {
            [FFSearchBar resignFirstResponder];
            }

            -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *) searchText{
            if (searchText.length ==0){
            isFiltered =NO; }
            else{

            isFiltered =YES;

            filteredObjects=[[NSMutableArray alloc]init];
            for(PFObject *object in allObjects) {
                NSString *str = [object objectForKey:FF_USER_NOMECOGNOME];

                NSRange stringRange =[str rangeOfString:searchText options:NSCaseInsensitiveSearch];

                if (stringRange.location !=NSNotFound) {
                    [filteredObjects addObject:object];

                }

            }


            }

            [self.FFTableViewFindUser reloadData];
            }

            //table view datasource and delegate method.....

            -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section
            {
            if (isFiltered){
            return [filteredObjects count];
            }return [allObjects count];

            }

            -(BOOL)IsFriend:(PFUser *)user {
            for (PFUser *friend in self.filtroamici) {
            if ([friend.objectId isEqualToString:user.objectId]) {
                return YES;
            }
            }
            return NO;
            }

            -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            static NSString *CellIdentifier = @"CellFindUser";


            FFCellFindUser *cell= [self.FFTableViewFindUser dequeueReusableCellWithIdentifier: CellIdentifier];
            if (!cell){
            cell=[[FFCellFindUser alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }




            if(!isFiltered ){


            PFObject *object = [allObjects objectAtIndex:indexPath.row];
            NSString *str = [object objectForKey:FF_USER_NOMECOGNOME];

            cell.FFLabelCell_NomeCognome.text = str;


            cell.FFIMGCell_FotoProfilo.image = [UIImage imageNamed:@"FFIMG_Camera"];
            [cell.FFIMGCell_FotoProfilo.layer setMasksToBounds:YES];
            [cell.FFIMGCell_FotoProfilo.layer setCornerRadius:22.5f];
            cell.FFIMGCell_FotoProfilo.file = [object objectForKey:FF_USER_FOTOPROFILO];
            [cell.FFIMGCell_FotoProfilo loadInBackground];

            cell.FFUserButton.tag = indexPath.row   ;
            [cell.FFUserButton addTarget:self action:@selector(FFInviaRichiestaAmicizia:) forControlEvents:UIControlEventTouchUpInside];

               [cell.FFUserButton setHidden:([self.filtroamici containsObject: object.objectId])];



            }    else

            {

            PFObject *object = [filteredObjects objectAtIndex:indexPath.row];
            NSString *str = [object objectForKey:FF_USER_NOMECOGNOME];

            cell.FFLabelCell_NomeCognome.text = str;
            cell.FFIMGCell_FotoProfilo.image = [UIImage imageNamed:@"FFIMG_Camera"];
            [cell.FFUserButton setBackgroundColor:[UIColor colorWithRed:(203/255.0) green:(120/255.0) blue:(71/255.0) alpha:(1)]];
            [cell.FFUserButton.layer setMasksToBounds:YES];
            [cell.FFUserButton.layer setCornerRadius:5.0f];

            cell.FFIMGCell_FotoProfilo.file = [object objectForKey:FF_USER_FOTOPROFILO];
            [cell.FFIMGCell_FotoProfilo loadInBackground];




            }






            return cell;
            }




            -(IBAction) FFInviaRichiestaAmicizia:(id) sender{
            NSLog(@"The row id is %d", [sender tag]);

            if (!isFiltered) {

            PFRelation*relation = [[PFUser currentUser] relationforKey:@"Friend"];

            PFUser *user = [self.allObjects objectAtIndex:[sender tag]];
            [relation addObject:user];
            [[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (error) {
                    NSLog(@"errore");
                }
                     }];
            }

            /*  PFObject *ObjectUser = [allObjects objectAtIndex:[sender tag]];
            PFObject *FFInviaRichiestaAmicizia = [PFObject objectWithClassName:@"Amicizie"];
            // FFInviaRichiestaAmicizia[@"STATO"] = @"Richiesta in Attesa";
            FFInviaRichiestaAmicizia[@"DA"] = [PFUser currentUser];
            FFInviaRichiestaAmicizia[@"A"] = ObjectUser;
            [[PFUser currentUser] incrementKey:@"conto"];
            [FFInviaRichiestaAmicizia setValue:[NSNumber numberWithBool:YES] forKey:@"RICHIESTA_IN_ATTESA"];
            [FFInviaRichiestaAmicizia saveInBackground];*/



            //} else {

            /* PFObject *ObjectUser = [filteredObjects objectAtIndex:[sender tag]];
            PFObject *FFInviaRichiestaAmicizia = [PFObject objectWithClassName:@"Amicizie"];
            // FFInviaRichiestaAmicizia[@"STATO"] = @"Richiesta in Attesa";
            FFInviaRichiestaAmicizia[@"DA"] = [PFUser currentUser];
            FFInviaRichiestaAmicizia[@"A"] = ObjectUser;
            [[PFUser currentUser] incrementKey:@"conto"];
            [FFInviaRichiestaAmicizia setValue:[NSNumber numberWithBool:YES] forKey:@"RICHIESTA_IN_ATTESA"];

            [FFInviaRichiestaAmicizia saveInBackground];*/
            //  }

            }
Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Is this question related to your other post: https://teamtreehouse.com/forum/searchbar-in-uitableview-using-parsecom?

These seem a little out of context. Can you make your entire project available via DropBox or put it up on GitHub?