【问题标题】:how to set UITextView height dynamically inside a custom UITableViewCell with autolayout如何使用自动布局在自定义 UITableViewCell 内动态设置 UITextView 高度
【发布时间】:2013-12-20 14:33:20
【问题描述】:

我有 UITableView,每个 tableViewCell 都是自定义的。在我的 customTableViewCell 内部是一个 UITextView,TextViews 框架是 pin 或与其 superView 相同,即 tableViewCell。

我将如何动态设置与 TextViews 大小成正比的 UITableViewCell 高度,这也取决于我从互联网上获得的内容文本。

(样品原型)

// 这就是我操作每个单元格高度的方式

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    // How do I set the height here, whats the best approach for this. with autolayout BTW
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// I initialize every cell here with my ArrayContainer, nothing much to refer here.
}

【问题讨论】:

标签: ios objective-c uitableview autolayout


【解决方案1】:
  1. 关注这个答案:Link reference

  2. 为您的 UITextView 添加一个高度约束,并为您的自定义单元格类创建一个出口。

  3. 在UITextView 上使用sizeThatFits: 以获取适合内容的大小,并将此值设置为2 中描述的约束的constant。在tableView:heightForRowAtIndexPath: 中执行此操作。

需要注意的是,如果有很多单元(数百或数千),那么您可能会遇到性能问题。

【讨论】:

  • @Spail: 你能检查对sizeThatFits:的调用是否返回了正确的大小吗?
  • 是的,返回的尺寸有点奇怪。它的宽度大于应有的宽度。您可以为此发布您的代码吗?
  • 没关系,我忘了在高度上加上 +1 以说明单元格分隔符。好像现在可以了。谢谢。
【解决方案2】:

主要获取texts as NSArray 并将其临时分配到 UITextView 中,这样您将获得单元格的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        CGRect frame;
        //  set the height here, According to the NSArray of text 
        if(textView || section )
        UITextView *tempTxtView = [[UITextView alloc] init];
        tempTxtView.text = // Add the Text of index according to the Section
        // Fit  the  UITextView to the Content 
        frame = tempTxtView.frame;
        frame.size.height = tempTxtView.contentSize.height;
        tempTxtView.frame = frame;
    }    
    return frame.size.height;
 }

一旦得到了 UITableViewCell 的高度:那就不用担心这个了吧?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// I initialize every cell here with my ArrayContainer, nothing much to refer here.

self.textView =  [[UItextView alloc] initWithFrame:CGRectMake(cell.frame.origin.x +20, cell.frame.origin.y +10, cell.frame.size.width - 20, cell.frame.size.height - 20)];

// ....... Do further with the NSArray of Text


}

注意:我没有测试过,这只是一个想法。希望对你有帮助

【讨论】:

    【解决方案3】:
     - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
     {
          CGSize titlesize = [TxtValue.text sizeWithFont:[UIFont fontWithName:appdel.strFont size:25.0f] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];  /// ----- -set width as per your requirement inplace of 300 
    
          return titlesize.height+10;
     }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 2015-01-19
      • 2017-05-29
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      相关资源
      最近更新 更多