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
?
Sử dụng storyboards thưa ông, cuộc sống của bạn sẽ trở nên dễ dàng hơn. –
@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