繁体   English   中英

UITableViewCell内的UICollectionView,UITableView动态高度

[英]UICollectionView inside UITableViewCell , UITableView Dynamic Height

我有一个UITableViewController ,每个UITableViewCell包含UICollectionView其中的内容UICollectionView应该是完全可见。 我的问题是,当返回高度UITableViewCell我无法返回正确的高度,因为我无法弄清楚如何为其内容计算UICollectionViewcontentSize

是的,您可以做到。如果要动态查看表格高度,则需要为该表格设置估计高度。

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight

您必须确保单元格的高度与集合视图完全相同。在外部表视图中设置单元格时,请尝试在内部集合视图中重新加载数据,否则由于可重复使用的单元格逻辑API不会区分先前的集合视图。

更新:

我有一种情况,我必须将表格视图存储在单元格中。

内部单元格看起来像是存储表视图的地方。

   @IBOutlet weak var uploadedDocumentsTableView: UITableView! // has 0 to left right top botton margin to superview on storyboard prototype (Contenview)
   @IBOutlet weak var tableViewCellHeight: NSLayoutConstraint! //To constraint the height of the tableview inside the cell.


  //This function is called when the outer tableview delegate calls cellForRowAtIndexPath to deque a cell.
   override func setupWithCellModel(cellModel: CRCustomerAreaDetailsCellModel)   {

    let castedCellModel = cellModel as? CRCustomerAreaDocumentUploadCellModel
    if  let documents = castedCellModel?.docUploadItem?.documents {
        tableViewCellHeight.constant = CGFloat(documents.count * 60)
    }   
    uploadedDocumentsTableView.reloadData()
  }

普通

extension CRCustomerAreaDocumentUploadV2GalleryCell: UITableViewDataSource {

public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 60
}

public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let castedCellModel = cellModel as? CRCustomerAreaDocumentUploadCellModel
    if  let documents = castedCellModel?.docUploadItem?.documents {
        return documents.count
    }
    return 0
}

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    ...
   //Return your cell
}

}

我的外部tableview的高度是通过自动尺寸计算的,但是对于内部tableview,我需要自己指定高度。

我已经读过很多次了。 您不应该将CollectionView放在TableViewCell内。 这可能会引起很多麻烦(例如掉帧)。 您是否尝试过使用StackView? 您是否尝试过AsyncDiplayKit?

抱歉,但是您不能使用UITableViewAutomaticDimension来确定内部带有UICollectionView的单元格的大小,因为集合视图没有正确的内部内容大小。 您应该重载UITableViewDelegate的height方法:

- (CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath;

并从此方法返回正确的高度。

另一种方法是创建自定义视图,该视图包含UICollectionView并控制集合视图的内容(来自我的代码的示例):

- (CGSize)intrinsicContentSize
{
    CGSize size = CGSizeMake(UIViewNoIntrinsicMetric, self.collectionView.collectionViewLayout.collectionViewContentSize.height);

    return size;
}

- (void)addTags:(NSArray<BLTag*>*)tags
{
    [_tagsList addObjectsFromArray:tags];

    [self _reloadData];
}

该视图返回正确的固有内容大小,因此您可以将其插入具有自动尺寸高度的单元格中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM