2013-07-22 23 views
10

Tôi có một số UICollectionView đã được tạo theo chương trình. Khi tạo chế độ xem bộ sưu tập, tôi muốn tự động xác định chiều cao xem bộ sưu tập dựa trên số lượng ô mà nó phải giữ. Tôi không muốn cho phép cuộn chế độ xem bộ sưu tập, thay vào đó chế độ xem bộ sưu tập này đang được thêm làm chế độ xem phụ cho chế độ xem được chứa bên trong một đường thẳng đứng UIScrollView.Tự động đặt ContentSize của UICollectionView

Ví dụ: nếu UICollectionView có 10 UICollectionViewCells. Nó có thể có chiều cao 200,0f, nhưng nếu nó có 20 ô, nó có thể có chiều cao 300,0f, v.v.

Tôi đã cố gắng thực hiện việc này bằng cách làm theo tài liệu của Apple here, ghi đè phương pháp collectionViewContentSize.

Mặc dù phương thức này trả về CGSize hợp lệ khi được gọi, khi tôi khởi tạo chế độ xem bộ sưu tập, frame của nó luôn được đặt thành 0.

Dưới đây là những gì tôi đã làm cho đến nay:

//subclass UICollectionViewFlowLayout 

@interface LabelLayout : UICollectionViewFlowLayout 

@property (nonatomic, assign) NSInteger cellCount; 
@property (nonatomic) UIEdgeInsets sectionInset; 
@property (nonatomic) CGSize itemSize; 
@property (nonatomic) CGFloat minimumLineSpacing; 
@property (nonatomic) CGFloat minimumInteritemSpacing; 

@end 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     [self setup]; 
    } 

return self; 
} 

-(void)prepareLayout 
{ 
    [super prepareLayout]; 
    _cellCount = [[self collectionView] numberOfItemsInSection:0]; 
} 

- (void)setup 
{ 
    self.sectionInset = UIEdgeInsetsMake(10.0f, 0.0f, 10.0f, 0.0f); 
    self.itemSize = CGSizeMake(245.0f, 45.0f); 
    self.minimumLineSpacing = 10.0f; 
    self.minimumInteritemSpacing = 20.0f; 
} 

- (CGSize)collectionViewContentSize 
{ 
    CGFloat collectionViewWidth = 550; 
    CGFloat topMargin = 10; 
    CGFloat bottomMargin = 10; 
    CGFloat collectionViewHeight = (self.cellCount * (self.itemSize.height +  
    self.minimumLineSpacing*2)) + topMargin + bottomMargin; 

    //THIS RETURNS A VALID, REASONABLE SIZE, but the collection view frame never gets set with it! 
    return CGSizeMake(collectionViewWidth, collectionViewHeight); 
} 

//create collectionView in viewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self makeLabels]; //determines the number of cells 

    LabelLayout *layout = [[LabelLayout alloc]init]; 
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout]; 
    self.collectionView.backgroundColor = [UIColor redColor]; 

    self.collectionView.dataSource = self; 
    self.collectionView.delegate = self; 

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier]; 
    [self.collectionView reloadData]; 
    [self.view addSubview:self.collectionView]; 
} 

Thêm vào đó, khi tôi xác định một cách rõ ràng một khung tĩnh cho UIcollectionView, nó được tạo ra như mong đợi, vì vậy tôi biết rằng vấn đề duy nhất của tôi là trong việc sử dụng collectionViewContentSize.

Vì vậy, câu hỏi của tôi là, làm cách nào để tự động đặt chiều cao của UICollectionView?

+0

Sử dụng storyboards thưa ông, cuộc sống của bạn sẽ trở nên dễ dàng hơn. –

+0

@AdamWaite, hãy cho tôi biết làm thế nào tôi có thể làm điều đó, tôi đang sử dụng bảng phân cảnh cho nó – RaviJSS

Trả lời

8

Chế độ xem bộ sưu tập chế độ xem cuộn, do đó phương pháp -collectionViewContentSize của bạn đang xác định kích thước của nội dung chứ không phải kích thước của chế độ xem tổng thể. Bạn sẽ cần đặt thuộc tính bounds hoặc frame của chế độ xem bộ sưu tập để đặt kích thước của chế độ xem bộ sưu tập.

Bạn cũng có thể muốn đặt thuộc tính scrollEnabled thành NO khi bạn ở đó.

+0

đã nhận nó, cảm ơn. Tôi đã làm điều này để làm cho nó hoạt động: CGFloat cellHeight = 45; Chiều cao CGFloat = [số tự đếm] * cellHeight; CGFloat width = 550; _collectionView.frame = CGRectMake (0, 0, chiều rộng, chiều cao); – jac300

+0

Có vẻ đúng. – Caleb

1

Sử dụng phương pháp sizeForItemAtIndexPath:.

Trong Swift:

func collectionView(_ collectionView: UICollectionView, 
    layout collectionViewLayout: UICollectionViewLayout, 
    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{ 
    return CGSizeMake(250, 150); 
} 

Trong Objective-C:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    return CGSizeMake(250, 150); 
} 
Các vấn đề liên quan