2017-09-22 16 views
0

Je suis conscient que je ne peux pas enregistrer un indexPath dans NSUserDefaults. Je veux sauver les cellules que l'utilisateur sélectionne ainsi quand l'utilisateur ouvre le contrôleur de vue ENCORE - les cellules sélectionnées dans la vue de collection sont AFFICHÉES. Cependant, le code ci-dessous ne sauvegarde pas les cellules sélectionnées. Le premier bloc de code est censé enregistrer les cellules sélectionnées (mais ne le fait pas) et le deuxième bloc de code indique que ce code ne sera jamais exécuté (indiqué ci-dessous).NSUserDefaults n'enregistre pas indexPath pour les cellules sélectionnées

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [[NSUserDefaults standardUserDefaults] setObject:@{@"row": @(indexPath.row), 
                 @"section": @(indexPath.section)} 
               forKey:@"CollectionIndex"]; 

    [[NSUserDefaults standardUserDefaults] synchronize]; 
} 

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

    NSIndexPath *storedIndexPath = [[NSUserDefaults standardUserDefaults] objectForKey:@"CollectionIndex"]; 

    static NSString *identifier = @"cell"; 
    UICollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:storedIndexPath]; 

    myCell.contentView.backgroundColor = [UIColor purpleColor]; 
    myCell.layer.masksToBounds = YES; 
    myCell.layer.cornerRadius = 6; 

    return myCell; 

} 

Ma collection vue CODE ne s'éxécutera jamais

if (collectionView ==self.septView) { 
    // Call identifier 
    static NSString *identifier = @"cell"; 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    cell.layer.masksToBounds = YES; 
    cell.layer.cornerRadius = 6; 

    // cell.contentView.backgroundColor = [UIColor purpleColor]; 


    // Color 
    self.septView.backgroundColor = [UIColor clearColor]; 

    // Label 
    self.titles = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)]; 
    self.titles.tag = 0; 
    self.titles.textColor = [UIColor whiteColor]; 
    self.titles.textAlignment = NSTextAlignmentCenter; 
    self.titles.font = [UIFont fontWithName:@"Avenir" size:15]; 
    self.titles.text = [self.sept objectAtIndex: indexPath.row]; 
    [cell.contentView addSubview:self.titles]; 



    return cell; 
} 
+1

double possible de [Erreur lors de la tentative de sauver indexPath à NSUserDefaults] (https://stackoverflow.com/questions/21509911/error-when-trying-to-save-indexpath-to-nsuserdefaults) – the4kman

+0

J'ai utilisé ce code, mais il nev er travaillé. Mise à jour ma question cependant. Le NSIndexPath que j'ai créé et mis en œuvre ne fonctionne pas. La question que vous avez postée ne couvre pas. Je pense que j'ai écrit une mauvaise ligne de code sous 'CellForItemAtIndexPath' @ the4kman – Lisa

Répondre

0

Vous pouvez obtenir votre chemin d'index dans votre cellForItemAtIndexPath comme ceci:

NSDictionary *storedIndexDictionary = [[NSUserDefaults standardUserDefaults] objectForKey:@"CollectionIndex"]; 
    NSInteger row = [[storedIndexDictionary objectForKey:@"row"] integerValue]; 
    NSInteger section = [[storedIndexDictionary objectForKey:@"section"] integerValue]; 

    NSIndexPath *storedIndexPath = [NSIndexPath indexPathForRow:row inSection:section];