8

私は最初のアプリケーションを使用して構築してUICollectionViewいますが、オブジェクトの削除に関してできることはあまりないことに気づきました。アプリの場合UITableView、スワイプして削除する方法があります。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } 

}

を使用するGMGridViewと、iPhoneのホーム画面を長押しするのと同じような動作になります。ビューの星が揺れ、削除ボタンが表示され、ビューの削除が行われます。私は確かにこの振る舞いを再現しようと試みることができますが、ユーザーが「それを理解する」かどうかはわかりません。

ユーザーがオブジェクトを削除できるようにするためのオプションにUICollectionView興味があります-独自の削除ジェスチャ/コントロールを実装する必要がありますか、それとも不足している(またはオープンソース)ものがありますか?

4

4 に答える 4

7

このコードをCollectionViewを含むViewControllerに挿入し、この方法で実行しました。選択したセルを検出するために、タップジェスチャですでにこのようなことを行っている可能性があります。

- (IBAction)didLongPressCellToDelete:(UILongPressGestureRecognizer*)gesture {
CGPoint tapLocation = [gesture locationInView:self.myCollectionView];
NSIndexPath *indexPath = [self.myCollectionView indexPathForItemAtPoint:tapLocation];
if (indexPath && gesture.state == UIGestureRecognizerStateBegan) {
    NSLog(@"image with index %d to be deleted", indexPath.item);
    self.itemToBeDeleted = indexPath.item;
    UIAlertView *deleteAlert = [[UIAlertView alloc]
                             initWithTitle:@"Delete?"
                             message:@"Are you sure you want to delete this image?"
                             delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];
    [deleteAlert show];

}
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"selected button index = %d", buttonIndex);
if (buttonIndex == 1) {
    // Do what you need to do to delete the cell 
    [self.myCollectionView reloadData];
}
}
于 2013-05-01T02:29:50.270 に答える
6

デフォルトのUICollectionViewCellには、空白のビューのみがあります(タイトル、削除ボタン、imageViewはありません)

UICollectionViewCellをサブクラス化し、それに削除ボタンを追加します。表示する場合はsetHidden=NO(例:下にスワイプ)

カスタムデリゲートを使用してデータを削除し、collectionViewをリロードします

サンプルでは、​​右にスワイプしてUICollectionViewCellを削除し、storyBoardで垂直方向にスクロールします。

//Cell.h
@class MyCell;
@protocol MyCellDelegate
-(void)deleteButtonClick:(MyCell *)cell;
@end
@interface MyCell : UICollectionViewCell
@property (weak , nonatomic) id<MyCellDelegate> delegate;
@property (weak,nonatomic) IBOutlet *delButton;
@end

//Cell.m
-(void)awakeFromNib{
  [self.delButton setHidden:YES]
  //add Swipe right to here
}
-(void)handleSwipeRight:(UISwipeGestureRecognizer *)swipe {
   [self.delButton setHidden:NO];
}
-(IBAction)clickDelBut{
 [self.delegate deleteButtonClick:self];
}

//ViewController.m
//In cellForItemsAtIndexPath cell.delegate = self.

-(void)deleteButtonClick:(MyCell *)cell{
  //get indexPath, delete data and reload collectionView here
}
于 2013-03-26T01:33:55.413 に答える
0

UICollectionViewで物事を削除および編集するために私が見つけて実装しているのは、このブログ投稿からのものです。

基本的に、コピー/貼り付けなどのメニューを表示し、削除するための独自のアクションを追加します。これはiSangの回答に似ていますが、UICollectionViewsでうまく機能しないジェスチャレコグナイザーを追加していません。

これは、iOSの他の部分でテキストやリンクをコピーして貼り付けたいときに、人々が編集メニューを表示するためにすでに使用している長押しのジェスチャーを使用します。

http://paulsolt.com/2012/11/uicollectionview-custom-actions-and-uimenucontroller/

于 2013-04-22T22:31:43.523 に答える
0

将来これを読む人のために、これは私が使用している便利なライブラリです: https ://github.com/Raizlabs/RZUtils/tree/master/RZUtils/Components/RZCollectionTableView

UICollectionViewを使用してUITableViewを模倣するため、UITableViewに付属するすべての優れた編集機能を使用してコレクションビューの柔軟性を得ることができます。

ただし、別の種類の削除動作や別のレイアウトを実装しようとしている場合は、私が知る限り、最初から実装する必要があります。UIGestureRecognizersを使用してカスタム作業を行うのが初めての場合は、このチュートリアルをお勧めします: http ://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more

お役に立てば幸いです。

于 2014-11-08T20:04:19.800 に答える