23

があり、正常に動作しますが、プログラムでコレクションビューにUICollectionViewいくつかのアイテムを追加したいと思います。UICollectionViewCells

では、どうすればこれを達成できますか?

さらに明確にするために、プログラムで言うときは、アプリがロードされたときではなく、アクションが実行されたときに(viewDidLoadメソッドを使用して)実行時にセルを挿入することを意味します。UICollectionViewモデルが更新され、insertItemsAtIndexPaths:メソッドで呼び出されるのはいつかわかります。新しいセルを作成する必要がありますが、それは行われず、エラーがスローされます。

4

4 に答える 4

26

... UICollectionViewのドキュメントを参照する

あなたは達成することができます:

セクションおよびアイテムの挿入、削除、および移動単一のセクションまたはアイテムを挿入、削除、または移動するには、次の手順に従います。

  1. データソースオブジェクトのデータを更新します。
  2. コレクションビューの適切なメソッドを呼び出して、セクションまたはアイテムを挿入または削除します。

コレクションビューに変更を通知する前に、データソースを更新することが重要です。コレクションビューメソッドは、データソースに現在正しいデータが含まれていることを前提としています。そうでない場合、コレクションビューは、データソースから間違ったアイテムのセットを受信するか、そこにないアイテムを要求してアプリをクラッシュさせる可能性があります。プログラムで単一のアイテムを追加、削除、または移動すると、コレクションビューのメソッドは、変更を反映するアニメーションを自動的に作成します。ただし、複数の変更を一緒にアニメーション化する場合は、ブロック内ですべての挿入、削除、または移動の呼び出しを実行し、そのブロックをperformBatchUpdates:completion:メソッドに渡す必要があります。次に、バッチ更新プロセスにより、すべての変更が同時にアニメーション化され、挿入、削除、

質問から:たとえば、ジェスチャー認識機能を登録し、次のようにして新しいセルを挿入できます。

// in .h 
@property (nonatomic, strong) NSMutableArray *data;

// in .m 
@synthesize data
// 

- (void)ViewDidLoad{
      //.... 

    myCollectonView.dataSource = self;
    myCollectionView.delegate = self; 
    data = [[NSMutableArray alloc] initWithObjects:@"0",@"1", @"2" @"3", @"4", 
                                                   @"5",@"6", @"7",  @"8", @"9",  
                                                   @"10", @"11", @"12", @"13",
                                                   @"14", @"15", nil];

    UISwipeGestureRecognizer *swipeDown = 
     [[UISwipeGestureRecognizer alloc] 
       initWithTarget:self action:@selector(addNewCell:)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
        [self.view addGestureRecognizer:swipeDown];
    //.. 
} 


-(void)addNewCell:(UISwipeGestureRecognizer *)downGesture {
    NSArray *newData = [[NSArray alloc] initWithObjects:@"otherData", nil];
    [self.myCollectionView performBatchUpdates:^{
        int resultsSize = [self.data count]; //data is the previous array of data
        [self.data addObjectsFromArray:newData];
        NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];

        for (int i = resultsSize; i < resultsSize + newData.count; i++) {
            [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i 
                                                              inSection:0]];
        }
        [self.myCollectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
    } completion:nil];

}
于 2013-10-22T15:28:15.437 に答える
12

に複数を挿入する場合itemsUICollectionViewperformBatchUpdates:

[self.collectionView performBatchUpdates:^{
    // Insert the cut/copy items into data source as well as collection view
    for (id item in self.selectedItems) {
        // update your data source array
        [self.images insertObject:item atIndex:indexPath.row];
        
        [self.collectionView insertItemsAtIndexPaths:
          [NSArray arrayWithObject:indexPath]];          
    }
} completion:nil];
于 2013-03-22T14:23:18.450 に答える
5

– insertItemsAtIndexPaths:仕事をします

于 2013-03-22T14:23:12.120 に答える
4

Swift3にアイテムを挿入する方法は次のとおりです。

    let indexPath = IndexPath(row:index, section: 0) //at some index
    self.collectionView.insertItems(at: [indexPath])

最初にデータを更新する必要があります。

于 2017-04-14T13:12:36.177 に答える