2012-10-28 5 views
21

Tôi mới đến UICollectionView và tôi đang làm theo hướng dẫn mà tôi tìm thấy trên web nhưng tôi bị kẹt do lỗi mà tôi không thể tìm ra. Đây là một chút ngữ cảnh.UICollectionView phải được khởi tạo với thông số bố cục không phải là số

Trong debugger Tôi có thể thấy rằng sau đây đang xảy ra:

  • numberOfSectionsInCollectionView: được gọi và tôi trở về 1
  • collectionView:numberOfItemsInSection: được gọi và tôi trở về kích thước của mô hình (20)
  • collectionView:layout:sizeForItemAtIndexPath: được gọi một lần cho mỗi mục trong mô hình
  • collectionView:layout:insetForSectionAtIndex: được gọi là
  • collectionView:cellForItemAtIndexPath: g ETS gọi và nó bị treo trên đường dây này ...

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; 
    

với lỗi này ...

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' 

Khi tôi tạm dừng thực hiện trên dòng đó và đánh dấu vào giao diện điều khiển dường như có bố cục ...

(lldb) po collectionView.collectionViewLayout 
(UICollectionViewLayout *) $4 = 0x07180fd0 <UICollectionViewFlowLayout: 0x7180fd0> 

UICollectionView là một phần của cảnh một và duy nhất trong bảng phân cảnh. Trong viewController.m không có UICollectionView s được tạo bởi bất kỳ phương tiện nào.

Có ai có ý tưởng nào không?

+2

lỗi đó đến khi bạn thực hiện một cái nhìn bộ sưu tập mới sử dụng alloc/init hoặc mới , thay vì thêm đối tượng bố cục khi bạn khởi tạo. Bạn đang tạo chế độ xem bộ sưu tập mới ở bất kỳ đâu? – jrturton

+1

@jrturton Không phải là tôi biết. UICollectionView là một phần của cảnh duy nhất và duy nhất trong bảng phân cảnh. Trong viewController.m không có UICollectionView được tạo bởi bất kỳ phương tiện nào. Nhưng với điều đó đã nói, và mới mẻ đối với chế độ xem bộ sưu tập, tôi không chắc chắn vị trí được liên kết với chế độ xem bộ sưu tập. –

Trả lời

18

Vì nó chỉ ra vấn đề là với registerClass:. Tôi có điều này:

[self.collectionView registerClass:[UICollectionView class] 
     forCellWithReuseIdentifier:@"MyCell"]; 

nhưng nó cần phải có được điều này:

[self.collectionView registerClass:[UICollectionViewCell class] 
     forCellWithReuseIdentifier:@"MyCell"]; 

Vì vậy, các phương pháp dequeue đã tạo ra một UICollectionView thay vì một UICollectionViewCell.

31

này đã làm việc cho tôi:

UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init]; 
    [aFlowLayout setItemSize:CGSizeMake(200, 140)]; 
    [aFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; 
myCollectionViewController = [[MyCollectionViewController alloc]initWithCollectionViewLayout:flowLayout]; 

Nếu bạn đang tạo UICollectionView lập trình một cách bố trí là bắt buộc.

+1

xin lỗi, không phải vậy. Như tôi đã đề cập trong câu hỏi, điều này sắp ra khỏi bảng phân cảnh và như được hiển thị trong đầu ra của bàn điều khiển có bố cục hợp lệ cho chế độ xem bộ sưu tập. Xem câu trả lời của tôi cho những gì vấn đề hóa ra được. –

+0

Vâng đó cũng là điều cần thiết. Bạn đã có một lớp sai giao cho tế bào. –

8

Nếu bạn đang tạo bộ điều khiển chế độ xem bộ sưu tập theo lập trình trong UICollectionViewController, hãy đảm bảo rằng Phương thức UICollectionViewController init sử dụng [super initWithCollectionViewLayout] thay vì [super init], ví dụ::

-(id) initWithImages:(NSArray *)pImages { 
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 
    [layout setMinimumInteritemSpacing:0.0f]; 
    [layout setMinimumLineSpacing:0.0f]; 
    layout.scrollDirection = UICollectionViewScrollDirectionVertical; 
    if (self = [super initWithCollectionViewLayout:layout]) { 
     _images= [[NSArray alloc] initWithArray:pImages]; 
    } 
    return self; 
} 

Từ: UICollectionViewController Class Reference:Overview

3

tôi đã nhận cùng một vấn đề và tôi đã giải quyết bằng cách sử dụng

UICollectionViewFlowLayout *myLayout = [[UICollectionViewFlowLayout alloc]init]; 
[myLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; 
[myLayout setMinimumInteritemSpacing:0.0f]; 
[myLayout setMinimumLineSpacing:0.0f]; 
UICollectionView *myCollectionView = [[UICollectionView alloc]initWithFrame:viewfame collectionViewLayout:myLayout]; 

thay vì

UICollectionView *myCollectionView = [[UICollectionView alloc]initWithFrame:self.view.frame]; 
[myLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; 
[myLayout setMinimumInteritemSpacing:0.0f]; 
[myLayout setMinimumLineSpacing:0.0f]; 
[myCollectionView setCollectionViewLayout:myLayout]; 
0

tôi đã chạy vào vấn đề này khi trở về một đối tượng UICollectionViewLayout với itemSize bằng self.view.bounds trước khi khung nhìn ở trong itialized.

0

Nếu lớp học của bạn là Kế thừa từ UICollectionViewController hoặc bạn đang tạo UICollectionView lập trình sau đó trong khi đẩy lớp bạn có thể đặt collectionViewLayout như dưới đây

let homeViewController = HomeViewContoller(collectionViewLayout:UICollectionViewLayout())