【问题标题】:UITableView Dynamic Height not changing iOSUITableView 动态高度不改变 iOS
【发布时间】:2019-07-08 03:04:36
【问题描述】:

拖放 UITableViewHeader

你可以看一个橙色的 UITextView。

我将tableview的高度常数设置为零。

重新加载后UITableView的tableview总高度显示与之前相同(因为没有UITextView显示)

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.heightConstCommentBox.constant = 0;

    [self configureTableviewFooter];

}

-(void)configureTableviewFooter{
    //More button Configure

    height =  50 +[self recusiveDescription:self.viewFooter] ;

    // Assign new frame
    self.viewFooter.frame =  CGRectMake(self.viewFooter.frame.origin.x, 
    self.viewFooter.frame.origin.y,self.viewFooter.frame.size.width ,  height); // viewFooter is container view of tableFooterView

    self.tableView.tableFooterView = self.viewFooter;
    [self.tableView reloadData];

}

- (float )recusiveDescription:(UIView *)view
{
    NSString *s = @"";
    float height = 0;

    NSArray *subviews = [view subviews];
    if ([subviews count] == 0) return 0;

    for (UIView *subView in subviews) {

    height = height + subView.frame.size.height ;

        [self recusiveDescription:subView];
    }
    return height;
}

重新加载 tableview 后,table view 页脚大小没有变化。

【问题讨论】:

  • 1.首先检查this variable row height 问题。 2. 请粘贴更多关于您的UITableView 的代码,而不是您的控制器。

标签: ios objective-c uitableview


【解决方案1】:

在方法recusiveDescription:(UIView *)view 中,您使用子视图的frame 属性。但是对于自动布局,它是不正确的。要获取future size of the view,请致电:

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize 
        withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority 
              verticalFittingPriority:(UILayoutPriority)verticalFittingPriority;

此方法根据提供的约束优先级返回视图的最佳大小。

另外我不明白你为什么在recusiveDescription:中调用recusiveDescription而不使用结果值。

【讨论】:

    【解决方案2】:

    如果您使用自定义单元格作为表格,您可以使用以下内容。

    在以下代码中,将 postss[index.row].post 替换为您的 label content,+230 是您单元格的最小高度。 或者你可以说它在标签以外的其他视图的高度。

    仅当您有 1 个标签或 1 个文本字段、1 个文本视图时,自动尺寸方法才有效。

    并且不要固定标签的高度。

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let heightOfRow = self.calculateHeight(inString: postss[indexPath.row].post)
        return (heightOfRow + 230)
    }
    
    //custom function for calculate dynamic height
    func calculateHeight(inString:String) -> CGFloat
    {
        let messageString = inString
        let attributes : [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15.0)]
    
        let attributedString : NSAttributedString = NSAttributedString(string: messageString, attributes: attributes)
    
        let rect : CGRect = attributedString.boundingRect(with: CGSize(width: 370, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil)
    
        let requredSize:CGRect = rect
        return requredSize.height
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-30
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      相关资源
      最近更新 更多