Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 所选单元格未在UICollectionView中持久化。无法保存。***由于未捕获异常而终止应用程序_Ios_Objective C_Delegates_Xib_Uicollectionreusableview - Fatal编程技术网

Ios 所选单元格未在UICollectionView中持久化。无法保存。***由于未捕获异常而终止应用程序

Ios 所选单元格未在UICollectionView中持久化。无法保存。***由于未捕获异常而终止应用程序,ios,objective-c,delegates,xib,uicollectionreusableview,Ios,Objective C,Delegates,Xib,Uicollectionreusableview,我有一个UICollectionView,其中填充了数据库中的数据。用户选择各种单元格并按下按钮保存信息。我无法获取要发布的数据。我做错了什么 #pragma mark <UICollectionViewDataSource> - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { NSLog(@"itemPresets --> %lu",(unsi

我有一个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];

        }

    }];
}
不确定为什么我会先得到两次0,但上面的nslog是正确的

正如您从上面的NSLogs中所看到的,最初选择并正确记录了该值,但没有保留该值(尽管它保持高亮显示),因此当用户按下按钮并调用register方法时,该值都为空。无法保存用户提供的信息。应用程序会触发警报(如果删除了对nil的检查,则会导致崩溃)。PostActivity正在工作并按预期保存到数据库,但类的Post应用中断

我试过各种各样的方法。我收到的错误(当签出时再次出现)包括:

int main(int argc,char*argv[]){ @自动释放池{ 返回UIApplicationMain(argc、argv、nil、NSStringFromClass([AppDelegate类]); } }
***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法对PFObject上的键或值使用nil。”。对值使用NSNull。”

我试过了

还将itemCell和itemKeyA等的属性更改为strong或retain(和非原子)

我已尝试重新连接UIOutlet/情节提要

这是nib/xib问题吗?我承认没有完全理解如何确保正确设置


当您将值分配给与
PFObject
实例关联的键时,感谢您。您的值为
nil
。在Parse中,他们认为您正试图将与键关联的值置零。但是,我认为您意外地将
nil
值赋给它。所以,只是想弄清楚你想做什么。在分配任何值之前,您可以使用
if-else
检查值是否为
nil

谢谢。所以问题是,选择单元格后,其值不应再为零。它应该是self.itemPresets[indexPath.section][indexPath.row];当用户选择单元格时,它开始工作,但在我将其保存到parse之前,它会重置为nil。简而言之,我想做的是保存用户标签(即UICollectionView中选定单元格的值),以便在将来的查询中检索这些值。这也是正确的。我只是想说明你是如何做到这一点的。我很确定这是我的问题。我给一些物品起了相同的名字,后来又改变了它们。我试着把属性从强改为弱,但我相信前面的引用仍然完好无损。
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
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)