J'ai une UICollectionView qui remplit les données de ma base de données. L'utilisateur sélectionne différentes cellules et appuie sur un bouton pour enregistrer les informations. Je suis incapable d'obtenir les données à poster. Qu'est-ce que je fais mal?La cellule sélectionnée ne persiste pas dans UICollectionView. Impossible d'enregistrer. *** Termination de l'application en raison d'une exception non interceptée
#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])); } }
*** en raison de l'application Mettre fin exception uncaught 'NSInvalidArgumentException', la raison: « Impossible d'utiliser zéro pour les clés ou les valeurs sur PFObject. Utilisez NSNull pour les valeurs. '
J'ai essayé UICollectionView datasource methods not getting called, but are being set in the init
propriété a également changé de itemCell et itemKeyA, etc à forte ou conserver (et nonatomic)
J'ai essayé rebranchant le UIOutlet/story-board
Est-ce un problème nib/xib? J'admets ne pas comprendre comment s'assurer que c'est bien configuré.
Merci
Merci Lucas. Donc, le problème est que, après que la cellule est sélectionnée, sa valeur ne devrait plus être nulle. Il devrait être self.itemPresets [indexPath.section] [indexPath.row]; Et cela fonctionne au départ lorsque l'utilisateur sélectionne la cellule, mais ensuite il se réinitialise à zéro avant de pouvoir l'enregistrer en analyse. Réponse courte, ce que je veux faire est enregistrer les étiquettes d'utilisateur (c.-à-d. Les valeurs des cellules choisies de l'UICollectionView) ainsi je peux récupérer ces valeurs une future requête. – user2985200
c'est aussi vrai. J'essaie juste d'expliquer comment vous en arrivez au point. –
Je suis assez sûr que c'est mon problème https://learnappmaking.com/error-1-unrecognized-selector-sent-to-instance/. J'avais nommé certains éléments identiques et les ai depuis changés. J'ai essayé de changer les propriétés de faibles à fortes, mais je crois que la référence précédente est toujours intacte. – user2985200