UITolViewView里面的UIColViewView,UITableView动态高度

时间:2016-12-27 07:40:08

标签: ios objective-c iphone uitableview uicollectionview

我有一个$(function() { function reloadTable() { $.get( "table1.php", function( data ) { $( "#RealTime" ).html( data ); }); } reload = setInterval(reloadTable, 1000); }); ,每个UITableViewController包含一个UITableViewCellUICollectionView的内容应该完全可见。我的问题是,当返回UICollectionView的高度时,我无法返回正确的高度,因为我无法弄清楚如何计算UITableViewCell contentSize的内容。

3 个答案:

答案 0 :(得分:0)

是的,你可以这样做。如果你想要动态的桌面视图高度,你需要为tableview设置估计的高度。

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight

您必须确保您的单元格与集合视图具有完全相同的高度。当您在外部tableview中设置单元格时,尝试在内部集合视图中重新加载数据,否则由于可重用的单元格逻辑API将无法区分之前的集合视图。

更新:

我有一个案例,我必须在一个单元格中存储一个tableview。

内部单元格看起来像这样存储了tableview。

   @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,我需要自己指定高度。

答案 1 :(得分:0)

我已经读过这么多次了。您不应该将CollectionView放在TableViewCell中。它可能会导致很多麻烦(例如帧丢失)。您是否尝试过使用StackView?你试过AsyncDiplayKit吗?

答案 2 :(得分:0)

很抱歉,但您无法使用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];
}

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