0

У меня есть UICollectionView в обычном UITableViewCell, только UIImageView в UICollectionView. Я использую SDWebImage для загрузки изображения. Что я хочу, после загрузки изображения, как UICollectionView, так и contentView (UITableViewCell) обновить себя, чтобы показать все изображение. Например, если у меня есть три изображения в UICollectionView, я надеюсь, что высота collectionview's равна сумме высоты всех трех изображений. Вот код: TableView:У меня есть UICollectionView в пользовательском TableViewCell, но я не могу автоматически изменять размер CollectionView

- (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; 
} 

Я использую автоматическую раскладку и использовать кладку, чтобы управлять им. Но что бы я ни пытался исправить, он просто показал мне высоту в 20 футов.

пс: Как сделать CollectionViewCell рамки Фрейм

ответ

0

изображения Попробуйте рассчитать высота

CGheight высота = [self.contentView systemLayoutSizeFittingSize: UILayoutFittingCompressedSize] .height + 2;

добавьте это в пользовательскую ячейку ур и позвоните по этому вопросу из метода делегирования коллекции

+0

Во-первых, спасибо большое. Но я не могу понять, какое ячеечное я ставлю этот код? Tableviewcell или collectionviewcell? И какой метод делегирования viewview следует вызвать? –

+0

Поместите это в коллекцию viewcell и вызовите из - (CGSize) collectionView: (UICollectionView *) collectionView layout: (UICollectionViewLayout *) collectionViewLayout sizeForItemAtIndexPath: (NSIndexPath *) indexPath метод – user3306145

Смежные вопросы