【问题标题】:uiCollectionViewCell resize celluiCollectionViewCell 调整单元格大小
【发布时间】:2014-04-10 17:48:59
【问题描述】:

我不知道如何调整我的 UICollectionView 中的单元格的大小,当用户编写时其中的 UITextView 变大(就像我在下面的代码中所做的那样:)

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if([text isEqualToString:@"\n"]){
        float widthLabel = 0.0f;

        UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
            widthLabel = (screenWidth-6)-4-200;
        }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
            widthLabel = (screenHeight-24)-4-200;
        }
        textView.frame = CGRectMake(2, 0, widthLabel, textView.frame.size.height+10);

    }
    if(text.length == 0 && [[textView.text substringWithRange:range] isEqualToString:@"\n"]){
        float widthLabel = 0.0f;

        UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
            widthLabel = (screenWidth-6)-4-200;
        }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
            widthLabel = (screenHeight-24)-4-200;
        }
        textView.frame = CGRectMake(2, 0, widthLabel, textView.frame.size.height-10);

    }
    return YES;
}

我想让包含此 UITextView 的项目单元格相对于 UITextView 变得更大/更小。我无法通过调整单元格的新高度并使用 reloadData 来更改大小,因为它会使键盘消失并且用户无法再书写。

【问题讨论】:

  • 嗨,盖比,你找到解决办法了吗?我也面临同样的问题。

标签: ios uitextview uicollectionview


【解决方案1】:

我不确定,但我认为您可以尝试使布局无效并强制重绘 uicollectionview 布局。

看看这个:Resize UICollectionView cells after their data has been set

【讨论】:

  • 是的,我之前看过那个帖子。我试过了,但不幸的是,如果我在用户还在写的时候调用 invalidateLayout,这会影响写...
  • 有没有办法在没有 UITextView 的情况下实现相同的结果(即:有一个 UICollectionView 单元格包含使单元格适合文本大小的文本)?因为,UITextView 不应该随着它的内容而增长,对吧?
  • 如果您尝试增加单元格大小,这意味着您正在尝试更改集合视图的当前布局,这需要您调用 invalidateLayout 并因此关闭键盘。相反,您可以为弹出窗口设置动画,接受用户输入,然后根据该单元格的内容重绘整个布局。
  • 谢谢,我就是这么想的。感谢您的帮助
猜你喜欢
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
相关资源
最近更新 更多