2014-01-30 82 views
2

我在使用indexPath的UICollectionView中使用了一個數組,我想在ActionSheet中使用具有相同indexPath的同一個數組。有沒有辦法以任何方式保存indexPath並將其加載到actionSheet中?保存在ActionSheet中使用的UICollectionView的索引路徑

這是我使用的代碼(我用更多的代碼爲行動片,當然,它可能workes,我只是附着問題所在的代碼):

- (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"); 
    } 
} 
+0

你想刪除使用indexPath /修改選定項目(S) UICollectionView,使用ActionSheet?如果是這樣,那麼你總是可以使用[self.collectionView indexPathsForSelectedItems]獲得所選項目的索引路徑; –

+0

@MattBecker它似乎並沒有工作..我正在使用UICollectionView不是集合,它被拖拽到一個普通的視圖控制器,而不是一個集合視圖控制器,如果這改變了情況。 – ThomasGulli

+0

它不。集合視圖控制器只是一個包含UICollectionView和其他東西的視圖控制器。如果它返回零,那麼你沒有選擇任何單元格。是否允許選擇或允許MultipleSelection設置爲YES?你有代表團嗎?如果是這樣,你應該能夠捕獲使用collectionView:didSelectItemAtIndexPath: –

回答

0

如果您要存儲一個變量,請嘗試以下操作:

使用NSUserDefaults代碼是這樣的:

商店indexPath:

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

[[NSUserDefaults standardUserDefaults] synchronize]; 

檢索存儲的indexPath:在

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

之後,你是自由的,請你

+0

選擇使用這種方式來存儲和檢索索引路徑時,出現以下錯誤,@ vborra: libC++ abi.dylib:以未捕獲的異常終止鍵入NSException – ThomasGulli

+0

我更新了我的答案,以便更準確。 – virindh

相關問題