2013-04-18 20 views
3

UICollectionView에 표시되는 일련의 이미지가 있습니다. 사용자가 이미지를 탭하면 해당 이미지에 대해 몇 가지 옵션이있는 UIActionSheet이 생성됩니다. 그들 중 하나가 UICollectionView에서 사진을 제거하고 있습니다. 사용자가 UIActionSheet에서 제거 버튼을 선택하면 확인을 요청하는 경고보기가 나타납니다. 사용자가 예를 선택하면 사진이 제거됩니다.UICollectionView에서 항목 제거

내 문제는 UICollectionView에서 항목을 제거하려면 을 deleteItemsAtIndexPaths 이벤트로 전달해야합니다. 경고 확인의 didDismissWithButtonIndex 이벤트에서 최종 확인이 부여 되었기 때문에 선택한 이미지의 indexPathdeleteItemsAtIndexPaths 이벤트로 전달하는 방법을 알아낼 수 없습니다. 어떻게해야합니까? 여기

내 코드입니다 :

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    switch (buttonIndex) { 
     case 0: 
      deletePhotoConfirmAlert = [[UIAlertView alloc] initWithTitle:@"Remove Photo" 
                   message:@"Do you want to remove this photo?" 
                   delegate:self 
                 cancelButtonTitle:@"Cancel" 
                 otherButtonTitles:nil, nil]; 
      [deletePhotoConfirmAlert addButtonWithTitle:@"Yes"]; 
      [deletePhotoConfirmAlert show]; 

      break; 
     case 1: 
      NSLog(@"To Edit photo"); 
      break; 
    } 
} 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    if (alertView == deletePhotoConfirmAlert) { 
     if (buttonIndex == 1) { 
      // Permission to delete the button is granted here. 
      // From here deleteItemsAtIndexPaths event should be called with the indexPath 
     } 
    } 
} 

- (void)deleteItemsAtIndexPaths:(NSArray *)indexPaths 
{ 

} 

답변

9

가 왜 를 사용합니다 [self.collectionView indexPathsForSelectedItems]. 한 번에 여러 이미지를 삭제할 때이 작업을 수행했습니다.

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if (alertView == deletePhotoConfirmAlert) { 
    if (buttonIndex == 1) { 
     // Permission to delete the button is granted here. 
     NSArray *selectedItemsIndexPaths = [self.collectionView indexPathsForSelectedItems]; 

     // Delete the items from the data source. 
     [self deleteItemsFromDataSourceAtIndexPaths:selectedItemsIndexPaths]; 

     // Now delete the items from the collection view. 
     [self.collectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths]; 
    } 
    } 
} 

// This method is for deleting the selected images from the data source array 
-(void)deleteItemsFromDataSourceAtIndexPaths:(NSArray *)itemPaths { 
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet]; 
    for (NSIndexPath *itemPath in itemPaths) { 
    [indexSet addIndex:itemPath.row]; 
    } 
    [self.images removeObjectsAtIndexes:indexSet]; // self.images is my data source 
} 

편집은 내가 갖는

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    NSArray *indexpaths = [self.collectionView indexPathsForSelectedItems]; 
    DetailViewController *dest = [segue destinationViewController]; 
    dest.imageName = [self.images objectAtIndex:[[indexpaths objectAtIndex:0] row]]; 
} 
+0

는 ** 'SomeViewController'에 대한 눈에 띄는 @interface 라인'[자기 deleteItemsFromDataSourceAtIndexPaths에서 선택한 'deleteItemsFromDataSourceAtIndexPaths'** 오류를 선언하지 않는 : selectedItemsIndexPaths를 ]; – Isuru

+0

Sory 내가 포인트를 잊어 버렸습니다. __deleteItemsFromDataSourceAtIndexPaths__는 데이터 소스 배열에서 선택한 이미지를 삭제하는 사용자 정의 함수입니다. 직접 그곳에서 작업을 수행 할 수 있습니다. __deleteItemsAtIndexPaths를 호출하기 전에 데이터 소스 배열에서 이미지를 삭제하는 것이 중요합니다. __ –

+0

알 수 있습니다. 이제'removeObjectAtIndex'를 사용하여 데이터 소스 배열에서 이미지를 제거하려고합니다. 그러나 정수 값을 기대하기 때문에 어떻게'selectedItemsIndexPaths' 배열에서 그것을 얻을 수 있습니까? – Isuru