にUICollectionView
があり、UICollectionView
にはUIImageView
しかありません。私はを使用してイメージをロードします。イメージをロードした後、UICollectionView
とcontentView(UITableViewCell
)の両方のイメージをすべて表示するようにコンテンツビューを更新します。たとえば、私がUICollectionView
に3つの画像を持っているなら、collectionview's
の高さが、3つの画像の高さの総和に等しいことを望みます。 TableView
:カスタムTableViewCellにUICollectionViewがありますが、コレクションビューのサイズを自動調整できません
- (void)createPostDetailUI{
_postDetailTableView = [UITableView new];
_postDetailTableView.delegate = self;
_postDetailTableView.dataSource = self;
_postDetailTableView.estimatedRowHeight = 180;
_postDetailTableView.tableFooterView = [UIView new];
[_postDetailTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
_postDetailTableView.rowHeight = UITableViewAutomaticDimension;
[self makeConstraints];
}
- (void)makeConstraints{
[_postDetailTableView mas_makeConstraints:^(MASConstraintMaker *make) {
UIEdgeInsets padding = UIEdgeInsetsMake(0, 0, 50, 0);
make.edges.equalTo(self.view).insets(padding);
}];
}
- (void)requestPostData{
//some request code
[_commentsArray addObjectsFromArray:[PostCommentModel arrayWithResponseObject:responseObject][0]];
[_postDetailTableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _commentsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
PostDetailHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PostDetailHeaderTableViewCell" forIndexPath:indexPath];
cell = [[PostDetailHeaderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PostDetailHeaderTableViewCell"];
[cell updateCellWithModel:_postModel];
return cell;
}
のUITableViewCell(その内部UICollectionView):
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//some other views
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
layout.minimumLineSpacing = 4.0;
layout.estimatedItemSize = CGSizeMake(300, 300);
_postImageCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_postImageCollectionView.backgroundColor = [UIColor clearColor];
_postImageCollectionView.showsHorizontalScrollIndicator = NO;
_postImageCollectionView.autoresizesSubviews = YES;
[_postImageCollectionView registerClass:[PostDetailImageCollectionViewCell class] forCellWithReuseIdentifier:@"PostDetailImageCollectionViewCell"];
_postImageCollectionView.delegate = self;
_postImageCollectionView.dataSource = self;
[self.contentView addSubview:_postImageCollectionView];
[self makeConstraints];
}
return self;
}
- (void)makeConstraints{
//some other views' autolayout code
[_postImageCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_postContentLabel);
make.right.equalTo(_postContentLabel);
make.top.equalTo(_postContentLabel.mas_bottom).offset(14);
make.height.greaterThanOrEqualTo(@20);
make.bottom.equalTo(self.contentView).offset(-16);
}];
}
- (void)updateCellWithModel:(PostsModel *)model{
_imageArray = [model getImageList];
[_postImageCollectionView reloadData];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _imageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PostDetailImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PostDetailImageCollectionViewCell" forIndexPath:indexPath];
NSString *cellImageURL = _imageArray[indexPath.row];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:cellImageURL] placeholderImage:[UIImage imageNamed:@"icon_default"]];
return cell;
}
UICollectionViewCell:
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
_imageView = [UIImageView new];
_imageView.frame = self.contentView.frame;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:_imageView];
}
return self;
}
私は、自動レイアウトを使用し、それを管理するために石工を使用 はここにいくつかのコードです。しかし、私はそれを修正しようとすると、それはちょうど20ptの高さを表示します。
PS:高さ
CGheight高さ= [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]を算出するために、これを試してみてくださいCollectionViewCell
フレームフィット画像フレーム
から呼び出します。まずは、ありがとうございます。しかし、私はこのコードを置くセルの表示を理解できませんか?tableviewcellまたはcollectionviewcell?そして、私はどのcollectionviewデリゲートメソッドを呼び出す必要がありますか? –
これをcollectionviewcellに入れ、 - (CGSize)から呼び出すcollectionView:(UICollectionView *)collectionViewレイアウト:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath メソッド – user3306145