包含UIWebViews动态调整大小的单元格的UICollectionView

时间:2014-10-30 17:40:49

标签: ios swift uiwebview uicollectionview uicollectionviewcell

我正在寻找一种动态调整UICollectionViewCellUICollectionView s的方法。这些单元格包含一个填充了动态HTML内容的UIWebView。因此我以前不知道webview的高度。

我试图挂钩func webViewDidFinishLoad(webView: UIWebView)函数并动态调整它们,但这会破坏整个布局。因为细胞相互生长。

我要为iOS 7.1构建

有没有人有线索?

2 个答案:

答案 0 :(得分:1)

为UIWebView创建高度约束,并将约束作为插座添加到UICollectionViewCell。

重载方法webViewDidFinishLoad:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // Fit web content size.
    CGRect frame = webView.frame;
    CGRect oldFrame = frame;
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    webView.frame = oldFrame;

    // Set content web view height constraint to new content size.
    self.contentWebViewHeightConstraint.constant = fittingSize.height;
}

现在,当webView高度约束发生更改时,如果正确设置约束,则单元格高度应该会更改。

答案 1 :(得分:-1)

执行以下操作:

  1. 创建一个数组,按索引heightArray: [CGFloat]
  2. 存储单元格的高度
  3. func webViewDidFinishLoad(webView: UIWebView)返回时,您可以确定单元格的高度。使用单元格索引heightArray[indexPath.row] = height
  4. 将此高度保存到数组中
  5. 致电tableView.beginUpdates()tableView.endUpdates()。这将强制表格通过调用func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
  6. 重新计算单元格的高度
  7. 实施func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat以返回给定heightArray
  8. 的高度

    这是在UITableView中拥有动态单元格高度的一般大纲。试试看。

相关问题