0

I have a UICollectionView that populates with data from my DB. The user selects various cells and presses a button to save the info. I am unable to get the data to post. What am I doing wrong?


#pragma mark <UICollectionViewDataSource>



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    NSLog(@"itemPresets --> %lu",(unsigned long)[self.itemPresets count]);

    return [self.itemPresets count];

}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    NSLog(@"itemPresets[section] --> %lu",(unsigned long)[self.itemPresets[section] count]);

    return  [self.itemPresets[section] count];

}



- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

    UICollectionReusableView * view = nil;



    if ([kind isEqualToString:UICollectionElementKindSectionHeader])

    {

        ItemSectionHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader

                                                                           withReuseIdentifier:NSStringFromClass([ItemSectionHeaderView class])

                                                                                  forIndexPath:indexPath];

        header.captionLabel.text = self.headTitleArray[indexPath.section];

        view = header;

    }

    return view;

}





- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCell *cell;



    ItemCell *aCell = (ItemCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"itemCell" forIndexPath:indexPath];



    if (indexPath.section == 2) {

        aCell.label.backgroundColor = [Helper colorWithSetting:(self.itemPresets[indexPath.section][indexPath.row])];

        aCell.label.text = @"";

    }

    else

    {

        aCell.label.text = self.itemPresets[indexPath.section][indexPath.row];

        aCell.label.textAlignment = NSTextAlignmentCenter;

    }

    cell = aCell;



    cell.selectedBackgroundView.backgroundColor = [UIColor darkGrayColor];

    return cell;

}







- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    CGSize size;

    if (indexPath.section == 6)

    {

        size = CGSizeMake(140, 30);

    }

    else if (indexPath.section <= 5 && indexPath.section >= 0)

    {

        size = CGSizeMake(80, 30);

    }

    else

    {

        size = CGSizeMake(self.collectionView.frame.size.width, 150);

    }



    return size;

}



#pragma mark <UICollectionViewDelegate>



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

  NSArray * selectedRows = self.collectionView.indexPathsForSelectedItems;



  for (NSIndexPath * selectedRow in selectedRows) {

    if ((selectedRow.section == indexPath.section) && (selectedRow.row != indexPath.row)) {

      [self.collectionView deselectItemAtIndexPath:selectedRow animated:NO];

    }

  }

  switch (indexPath.section) {

    case 0:

      self.setting.itemKeyA = self.itemPresets[indexPath.section][indexPath.row];

      NSLog(@“ValueA in case -----> %@",self.itemPresets[indexPath.section][indexPath.row]);

      break;

    case 1:

      self.setting.itemKeyB = self.itemPresets[indexPath.section][indexPath.row];

      NSLog(@“ValueB in case -----> %@",self.itemPresets[indexPath.section][indexPath.row]);

      break;

    case 2:

        self.setting.itemKeyC = self.itemPresets[indexPath.section][indexPath.row];

      NSLog(@“ValueC in case -----> %@",self.itemPresets[indexPath.section][indexPath.row]);

      break;

    default:

      break;

  }



    NSLog(@“ValueA in set -----> %@",self.setting.itemKeyA);

    NSLog(@“ValueB in set -----> %@",self.setting.itemKeyB);

    NSLog(@“ValueC in set -----> %@",self.setting.itemKeyC);



}



# pragma mark - sync with Parse

- (void)register

{

    if (!self.setting.KeyA || !self.setting.KeyB || !self.setting.KeyC)

    {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" Request Incomplete" message:@"Please select all the necessary fields." delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", @"Ok") otherButtonTitles:nil, nil];

        [alert show];

        return;

    }



    PFObject *postActivity = [PFObject objectWithClassName:@"Activity"];

    [postActivity setObject:@“Apply" forKey:@“activityType"];

    [postActivity setObject:[PFUser currentUser] forKey:@"fromUser"]; // Set fromUser

    [postActivity saveInBackground];



    PFObject *post = [PFObject objectWithClassName:@“Apply"];

    [post setObject:[PFUser currentUser] forKey:@"User"]; // Set fromUser

    NSLog(@“keyA -----> %@",self.setting.KeyA);

    NSLog(@“keyB -----> %@“,self.setting.KeyB);

    NSLog(@“keyC -----> %@",self.setting.KeyC);

    [post setObject:self.setting.KeyA forKey:@“keyA"];

    [post setObject:self.setting.KeyB forKey:@“keyB"];

    [post setObject:self.setting.KeyC forKey:@“keyC"];



    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];

    [self.view addSubview:self.HUD];



    // Set determinate mode

    self.HUD.mode = MBProgressHUDModeIndeterminate;

    self.HUD.delegate = self;

    self.HUD.labelText = @"Applying  ...";

    self.HUD.dimBackground = YES;

    [self.HUD show:YES];



    [post saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

        if (succeeded) {

            [self.HUD hide:YES];



        [self performSegueWithIdentifier:@"unwindItemCollectionViewController" sender:self];

        }

    }];
}

The logs:

2015-10-15 14:12:10.530 x itemPresets --> 0
2015-10-15 14:12:10.530 x itemPresets --> 0
2015-10-15 14:12:10.674 x itemPresets --> 3
2015-10-15 14:12:10.674 x itemPresets[section] --> 9
2015-10-15 14:12:10.675 x itemPresets[section] --> 6
2015-10-15 14:12:10.675 x itemPresets[section] --> 10

not sure why I get 0 twice first, but the above NSLogs are correct

2015-10-15 14:12:11.434 x ValueA in case -----> Correct / Selected value
2015-10-15 14:12:11.435 x ValueA in set -----> (null)
2015-10-15 14:12:11.435 x ValueB in set -----> (null)
2015-10-15 14:12:11.436 x ValueC in set -----> (null)
2015-10-15 14:12:12.217 x ValueB in case-----> Correct / Selected value
2015-10-15 14:12:12.217 x ValueA in set -----> (null)
2015-10-15 14:12:12.218 x ValueB in set -----> (null)
2015-10-15 14:12:12.218 x ValueC in set -----> (null)
2015-10-15 14:12:14.516 x ValueC in case-----> Correct / Selected value
2015-10-15 14:12:14.517 x ValueA in set -----> (null)
2015-10-15 14:12:14.517 x ValueB in set -----> (null)
2015-10-15 14:12:14.518 x ValueC in set -----> (null)

As you can see from the NSLogs above, the value is getting selected initially and logs correctly but it is not retained (tho it stays highlighted) so that when the user presses the button and calls the register method the values are all null. The information the user has provided can not be saves. The app triggers an alert (or crashes is the check for nil is removed). PostActivity is working and saves as expected to database, but Post for the class Apply breaks.

I've tried a variety of fixes. Errors I get (again when check is commected out) include:

int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use nil for keys or values on PFObject. Use NSNull for values.'

I've tried UICollectionView datasource methods not getting called, but are being set in the init

Also changed property of itemCell and itemKeyA, etc to strong or retain (and nonatomic)

I've tried reconnecting the UIOutlet / storyboard

Is this a nib / xib issue? I admit to not fulling understanding how to ensure that's set up correctly.

Thanks

Community
  • 1
  • 1

1 Answers1

0

When you assign value to a key associate to an instance of PFObject. You have a case that your value is nil. In Parse, they think you are trying to nil out the value associated with a key. However, I think you accidentally assign nil value to it. So, just trying to figure out what you are trying to do. Before assigning any values, you can use if-else to check the whether it's nil or not.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
  • Thanks Lucas. So the problem is that the after the cell is selected it's value should no longer be nil. It should be self.itemPresets[indexPath.section][indexPath.row]; And it work initially when the user selects the cell, but then it is resetting to nil before I can save it in parse. Short answer, what I want to do is save the user tags (ie values of selected cells from the UICollectionView) so I can retrieve these values a future query. – user2985200 Oct 16 '15 at 05:39
  • that's also true. I am just trying to address how you get to the point. – Lucas Huang Oct 16 '15 at 08:01
  • I'm pretty sure this is my problem https://learnappmaking.com/error-1-unrecognized-selector-sent-to-instance/. I had named some items the same and have since changed them. I've tried changing the properties to weak from strong but believe former reference is still intact. – user2985200 Oct 16 '15 at 18:15