Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 动态集合视图单元格大小目标c?_Objective C_Uicollectionview_Constraints - Fatal编程技术网

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

Objective c 动态集合视图单元格大小目标c?,objective-c,uicollectionview,constraints,Objective C,Uicollectionview,Constraints,我正在处理目标c中的集合视图 我的问题是 1.我想根据内容大小更改单元格大小 2.如果单元格中没有图像,则“喜欢”和“评论”视图应位于上方(参考图像) 我改变了约束条件,就像 NSLayoutConstraint *newConstraint; if([image isEqualToString:@"no_image.jpg"] || [image isEqualToString:@"no_image.jpg"]){ cell.desc_ImgViewWidth.constant

我正在处理目标c中的集合视图

我的问题是

1.我想根据内容大小更改单元格大小 2.如果单元格中没有图像,则“喜欢”和“评论”视图应位于上方(参考图像)

我改变了约束条件,就像

 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];
在cellForItemAtIndexPath委托方法中。(如和注释视图最初在上方移动,但在重新加载单元格(如滚动等)后,约束无法正常工作)

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


如何正确执行此操作?

您可以使用
UICollectionViewLayout

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

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

您可以使用
UICollectionViewLayout

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

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

终于做到了这样,

 - (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更好,但不幸的是,在以前的设计中使用collection view之后,需求发生了这样的变化。所以,我努力了。

终于做到了这样

 - (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更好,但不幸的是,在以前的设计中使用collection view之后,需求发生了这样的变化。所以,我很挣扎。

这只是部分屏幕截图,但似乎
UITableView
应该是更好的选择。否则,您会看到:?使用两个单元格,一个有图像,另一个没有图像。但是对于自调整单元格内容大小,表视图是最佳选择。它只是部分屏幕截图,但似乎
UITableView
应该是更好的选择。否则,您会看到:?使用两个单元格,一个有图像,另一个没有图像。但对于单元格内容的自调整大小,表视图是最佳选择。我认为最简单的方法是通过约束。谢谢@KamleshShingarakhiya,我遵循了你的想法。我认为最简单的方法是通过约束。谢谢@KamleshShingarakhiya,我遵循了你的想法。