动态集合视图单元格大小 - 目标c?

时间:2018-02-22 12:34:51

标签: objective-c uicollectionview constraints

我正在研究目标c中的集合视图,

我的问题是

1.我想根据内容大小更改单元格大小 2.如果单元格中没有图像,那么评论视图应该在上面(参考图像)。enter image description here

我更改了

之类的约束
 NSLayoutConstraint *newConstraint;

 if([image isEqualToString:@"no_image.jpg"] || [image isEqualToString:@"no_image.jpg"]){

    cell.desc_ImgViewWidth.constant = 0;

    [cell.Descimgview setHidden:YES];

    newConstraint = [NSLayoutConstraint constraintWithItem:(cell.bottomConstraint).firstItem attribute:(cell.bottomConstraint).firstAttribute relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:(cell.bottomConstraint).secondItem attribute:(cell.bottomConstraint).secondAttribute multiplier:(cell.bottomConstraint).multiplier constant:(cell.bottomConstraint).constant];

}
else{

    cell.desc_ImgViewWidth.constant = 120;
    [cell.Descimgview setHidden:NO];

    newConstraint = [NSLayoutConstraint constraintWithItem:(cell.bottomConstraint).firstItem attribute:(cell.bottomConstraint).firstAttribute relatedBy:NSLayoutRelationEqual toItem:(cell.bottomConstraint).secondItem attribute:(cell.bottomConstraint).secondAttribute multiplier:(cell.bottomConstraint).multiplier constant:(cell.bottomConstraint).constant];

}
    [cell.contentView removeConstraint:(cell.bottomConstraint)];
    [cell.contentView addConstraint:newConstraint];

    [cell layoutIfNeeded];
    [[cell contentView] setFrame:[cell bounds]];
    [[cell contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

in cellForItemAtIndexPath委托方法。(喜欢和评论视图​​一开始就在上面移动,但是再次重新加载单元格之后,就像滚动等一样,约束不能很好地工作)

我想像这样移动并评论视图,并减少该特定单元格的单元格高度(参见下图)

enter image description here

如何正确地做到这一点?

2 个答案:

答案 0 :(得分:2)

您可以使用UICollectionViewLayout

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

         if imageView != nil
         {
            return CGSizeMake(width, height)
         }
         else
         {
            return CGSizeMake(width, height)
         }
    }

答案 1 :(得分:0)

终于实现了这样,

 - (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
   sizeForItemAtIndexPath:(NSIndexPath *)indexPath
     {

       NSDictionary* object = [_Data objectAtIndex:indexPath.item];

       NSString *image = [object valueForKey:@"image"];

       if([image isEqualToString:@"no_image.jpg"] || [image isEqualToString:@"no_image.jpg"]){

          CGRect screenRect = [[UIScreen mainScreen] bounds];
          CGFloat screenWidth = screenRect.size.width;
          float cellWidth = screenWidth;

          NSDictionary* object = [_Data objectAtIndex:indexPath.item];


          NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[SwiftHelper getEmojiText:[object valueForKey:@"description"]]];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Georgia" size:15.0] range:NSMakeRange(0, str.length)];

          CGSize sizeName = CGRectIntegral([str boundingRectWithSize:CGSizeMake(cellWidth-8, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil]).size;

          NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:[SwiftHelper getEmojiText:[object valueForKey:@"name"]]];
    [str1 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Georgia" size:15.0] range:NSMakeRange(0, str1.length)];

          CGSize sizeName1 = CGRectIntegral([str1 boundingRectWithSize:CGSizeMake(cellWidth-8, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil]).size;
          NSLog(@"%f,%f",sizeName.height,sizeName1.height);
         if(sizeName.height > 100){
            sizeName.height = 70;
         }
         return CGSizeMake(cellWidth, sizeName.height + sizeName1.height + 110);


     }
     else{

         CGRect screenRect = [[UIScreen mainScreen] bounds];
         CGFloat screenWidth = screenRect.size.width;
         float cellWidth = screenWidth;

         CGSize size = CGSizeMake(cellWidth, 182);

         return size;
     }

感谢您的建议朋友。

最好使用tableview,但不幸的是,在使用先前设计的集合视图后,需求发生了这样的变化。所以,我很挣扎。