2012-11-01 32 views
9

Tôi đang cố gắng thêm một số ô nữa vào một số hiện tại UICollectionView, đã được lấp đầy một số ô.Thêm UICollectionViewCell vào UICollectionView hiện tại

Tôi đã cố gắng sử dụng CollectionView reloadData nhưng có vẻ như tải lại toàn bộ collectionView và tôi chỉ muốn thêm nhiều ô hơn.

Ai đó có thể giúp tôi không?

Trả lời

6

Lớp UICollectionView có các phương pháp để thêm/xóa các mục. Ví dụ: để chèn một mục tại một số index (trong phần 0), sửa đổi mô hình của bạn cho phù hợp và sau đó làm:

int indexPath = [NSIndexPath indexPathForItem:index]; 
NSArray *indexPaths = [NSArray arrayWithObject:indexPath inSection:0]; 
[collectionView insertItemsAtIndexPaths:indexPaths]; 

Quan điểm sẽ làm phần còn lại.

+0

Vì vậy, khi lần đầu tiên sử dụng '[UICollectionView reloadData]' i nên lưu chỉ mục của ô cuối cùng được sử dụng, và khi tôi gọi phương thức để thêm nhiều ô, tôi nên sử dụng ? @chris –

+0

Bạn không cần '[UICollectionView reloadData]' nào cả khi thêm các mục. Chỉ cần làm 'insertItemsAtIndexPaths:' và đảm bảo rằng các cuộc gọi tiếp theo đến nguồn dữ liệu của bạn 'phương thức 'collectionView: numberOfItemsInSection:' và 'collectionView: cellForItemAtIndexPath:' phản ánh các thay đổi của bạn. – chris

+0

Tôi đã làm nó lúc đầu nó làm việc. Nhưng, khi các ô mới sẽ được hiển thị trên màn hình, tôi nhận được thông báo: 'Chấm dứt ứng dụng do ngoại lệ chưa được nhận 'NSRangeException', lý do: '- [__ NSCFArray objectAtIndex:]: index (36) vượt quá giới hạn (36)' ' @chris bạn có biết tôi đã làm gì sai không? –

5

Cách đơn giản nhất để chèn các tế bào mới cho UICollectionView mà không cần phải tải lại tất cả các tế bào của nó là bằng performBatchUpdates, có thể được thực hiện dễ dàng bằng cách làm theo các bước dưới đây.

// Lets assume you have some data coming from a NSURLConnection 
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *erro) 
{ 
     // Parse the data to Json 
     NSMutableArray *newJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 

     // Variable used to say at which position you want to add the cells 
     int index; 

     // If you want to start adding before the previous content, like new Tweets on twitter 
     index = 0; 

     // If you want to start adding after the previous content, like reading older tweets on twitter 
     index = self.json.count; 

     // Create the indexes with a loop 
     NSMutableArray *indexes = [NSMutableArray array]; 

     for (int i = index; i < json.count; i++) 
     { 
      [indexes addObject:[NSIndexPath indexPathForItem:i inSection:0]]; 
     } 

     // Perform the updates 
     [self.collectionView performBatchUpdates:^{ 

      //Insert the new data to your current data 
      [self.json addObjectsFromArray:newJson]; 

      //Inser the new cells 
      [self.collectionView insertItemsAtIndexPaths:indexes]; 

     } completion:nil]; 
} 
+0

phương pháp này biến mất các ô trước đó, sau đó tải lại và cuộn bộ sưu tậpXem lên trên cùng. –

Các vấn đề liên quan