我有一个 UICollectionView 填充我的数据库中的数据。用户选择各种单元格并按下按钮以保存信息。我无法获取要发布的数据。我究竟做错了什么?
#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])); } }
*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“不能将 nil 用于 PFObject 上的键或值。使用 NSNull 作为值。
我试过 UICollectionView 数据源方法没有被调用,但正在初始化中设置
还将 itemCell 和 itemKeyA 等的属性更改为强或保留(和非原子)
我试过重新连接 UIOutlet / storyboard
这是笔尖/xib问题吗?我承认没有完全理解如何确保正确设置。
谢谢