Question

I'm using an array in my UICollectionView that uses indexPath, and I would like to use the same array with the same indexPath in an ActionSheet. Is it any way to save the indexPath in any way and load it in the actionSheet?

This is the code I am using (I'm of course using more code for the Action sheet, and it workes probably, I just attached the code where the problems are):

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    UILabel *label = (UILabel *)[cell viewWithTag:100];
    UIImageView *favoriteImage = (UIImageView *)[cell viewWithTag:750];
  //  UIImageView *image = (UIImageView *)[cell viewWithTag:500];

    //Sound title
    label.text = [mainArray objectAtIndex:indexPath.row];

    //Single sound image
   // image.image = [UIImage imageNamed: [NSString stringWithFormat:@"%@.png", [array objectAtIndex:indexPath.row]]];

    // Removes background color in cells
    cell.backgroundColor = [UIColor colorWithRed:190.0/255 green:179.0/255 blue:59.0/255 alpha:0.0];



        if ([FavoriteArray containsObject: [mainArray objectAtIndex:indexPath.row]])
        {
            favoriteImage.alpha = 1;
        }




    return cell;
}

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    //Get the name of the current pressed button
    NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
    if  ([buttonTitle isEqualToString:@"Destructive Button"]) {
        if (self.favoriteAcitve == YES) {


            if ([FavoriteArray containsObject: [mainArray objectAtIndex:indexPath.row]]) // YES
            {
                [FavoriteArray removeObject:[mainArray objectAtIndex:indexPath.row]];
                [FavoriteArray writeToFile:listPath atomically:YES];

            } else {
                [FavoriteArray addObject:[mainArray objectAtIndex:indexPath.row]];
                [FavoriteArray writeToFile:listPath atomically:YES];
                NSLog(@"Count %i", [FavoriteArray count]);

          //  }}

        NSLog(@"Destructive pressed --> Delete Something");
    }
    if ([buttonTitle isEqualToString:@"Other Button 1"]) {
        NSLog(@"Other 1 pressed");
    }
    if ([buttonTitle isEqualToString:@"Other Button 2"]) {
        NSLog(@"Other 2 pressed");
    }
    if ([buttonTitle isEqualToString:@"Other Button 3"]) {
        NSLog(@"Other 3 pressed");
    }
    if ([buttonTitle isEqualToString:@"Cancel Button"]) {
        NSLog(@"Cancel pressed --> Cancel ActionSheet");
    }
}
Était-ce utile?

La solution

If you want to store a variable, try the following:

Use NSUserDefaults the code goes something like this:

Store the indexPath:

[[NSUserDefaults standardUserDefaults] setObject:indexPath.row forKey:@"nameForStoredVariableHere_row"];
[[NSUserDefaults standardUserDefaults] setObject:indexPath.sectionforKey:@"nameForStoredVariableHere_section"];

[[NSUserDefaults standardUserDefaults] synchronize];

Retrieve the stored indexPath:

 NSNumber *storedRow = [[NSUserDefaults standardUserDefaults] objectForKey:@"nameForStoredVariableHere_row"];
 NSNumber *storedSection = [[NSUserDefaults standardUserDefaults] objectForKey:@"nameForStoredVariableHere_section"];

After that you're free to use the indexPath as you please

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top