tableview 的行高没有改变,CollectionView 正在捕获所有未使用的 space 而不是在 tableViewCell 中调整自身大小

tableview's Row height is not changing , CollectionView is capturing all the unused space instead of resizing itself in the tableViewCell

我按照此 tutorial 将 UICollectionView 放入 UITableViewCell,在我的 UICollectionViewCell 中,有一个图像视图。因此,当我 运行 我的应用程序时, collection 视图不会同时在我的单元格中调整自身大小,我放置了一个文本视图,它根据内容调整自身大小,请参见下图:

在第一张图片中,我在顶部有一个文本视图,其中包含一些文本,在其下方(粉红色背景)是我的 collection 视图,其中带有绿色背景的是我的图像视图,如您所见,collection 视图占用了额外的 space 而不是像 Text View 那样减少自身。

在第二张图片中,您可以看到我的 textView 比之前有更多内容,因此它调整了自身大小,现在与 CollectionView 重叠

这是我的 TableViewCell:

    class TableViewCell: UITableViewCell {


    @IBOutlet var txtView: UITextView!
    @IBOutlet private weak var collectionView: UICollectionView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

//        collectionView.frame = self.bounds;
//        collectionView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func setCollectionViewDataSourceDelegate
        <D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>
        (dataSourceDelegate: D, forRow row: Int) {

        collectionView.delegate = dataSourceDelegate
        collectionView.dataSource = dataSourceDelegate
        collectionView.tag = row
        collectionView.reloadData()
    }


    var collectionViewOffset: CGFloat {
        get {
            return collectionView.contentOffset.x
        }

        set {

            collectionView.contentOffset.x = newValue

        }
    }


}

这是我的 collectionViewCell

    class CollectionViewCell: UICollectionViewCell {

    @IBOutlet var imgView: UIImageView!


    override func awakeFromNib() {

        self.setNeedsLayout()
//        
//        self.contentView.frame = self.bounds;
//        self.contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

    }


}

这是我的 TableviewController

        // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView,
                            numberOfRowsInSection section: Int) -> Int {
        return imageModel.count
    }

    override func tableView(tableView: UITableView,
                            cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell",
                                                               forIndexPath: indexPath) as! TableViewCell

        cell.txtView.text = txtArray[indexPath.row]

        return cell
    }

    override func tableView(tableView: UITableView,
                            willDisplayCell cell: UITableViewCell,
                                            forRowAtIndexPath indexPath: NSIndexPath) {

        guard let tableViewCell = cell as? TableViewCell else { return }

        tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)

        tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0


    }




    override func tableView(tableView: UITableView,
                            didEndDisplayingCell cell: UITableViewCell,
                                                 forRowAtIndexPath indexPath: NSIndexPath) {

        guard let tableViewCell = cell as? TableViewCell else { return }

        storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
    }





}



extension TableViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView,
                        numberOfItemsInSection section: Int) -> Int {

        return imageModel[collectionView.tag].count
    }



    func collectionView(collectionView: UICollectionView,
                        cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",
                                                                         forIndexPath: indexPath) as! CollectionViewCell



         cell.imgView.frame = CGRectMake(0, 0, collectionView.frame.width, collectionView.frame.height)

         cell.imgView.contentMode = .ScaleAspectFit


        //cell.addSubview(imageView)

         cell.imgView.image = ResizeImage(UIImage(named: imageModel[collectionView.tag][indexPath.item])!, targetSize: CGSizeMake( cell.imgView.frame.width ,  cell.imgView.frame.height))
        //imageView.image = UIImage(named: imageModel[collectionView.tag][indexPath.item])


        return cell
    }    

}

如何根据其中的内容使这个 collection 视图成为 AutoLayout 本身?我也试过这个:

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;

但没有用(我的 collection 视图消失了)如果有人知道如何做到这一点,请指导我..

当我在 table 视图单元格中使用 collection 视图时,我遇到了类似的问题。无论我做什么,我都无法让 table 视图自动调整大小,但 collection 视图可以。 Soo 而不是自动布局,我是使用代码来完成的。

我最终不得不在 collection 视图中计算 collection 视图 numberOfSections 中标签的大小,并使用委托将此高度传递给具有 tableView 的委托和数据源并重新加载 table 视图的适当行。

碰巧的是,collection视图数据源中的 numberOfSections 每次都会被调用,委托会调整 table 视图高度。 像这样的东西-

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    [self.delegate setViewHeight:[self getHeightForCellAtIndexPath:[NSIndexPath indexPathForRow:currentSelected inSection:0]]];

    return 1;
}

这应该给你一个大概的概念。

编辑:对不起,我误解了你之前的问题。以下是适合您的内容:

据我了解,您有一个带有标签视图的 table 视图单元格和其中的 collection 视图。

现在,在您的 table 视图单元格中,您应该向标签添加顶部、前导和尾随约束 space。现在在您的 collection 视图中将您的图像垂直放置在中心,并向单元格超级视图添加适当的顶部和底部。您的 collection 视图本身应该在前导、尾随、顶部到标签和底部到超级视图中具有 CONSTANT 值。还要为您的 collection 视图添加固定高度限制(假设您希望图像大小保持不变)。

现在假设视图控制器 A 是 table 视图的数据源,table 视图单元格是 collection 视图的数据源。

在您的 viewController A 中,您应该将 indexPath 处的行高写为 -

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGSize stringSize = [yourStringArray[indexPath.row] boundingRectWithSize:CGSizeMake(_yourCollectionView.frame.size.width, FLT_MAX)
                                                  options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                               attributes:@{NSFontAttributeName:[UIFont yourFont size:yourFontSize]} context:nil].

    return stringSize.height + 35; //magic number 35, play around with it to suit your need. Did this to always have a minimum fixed height.
}

这将允许您的 tableViewRowForHeight 为该特定索引添加标签的高度,其余的应该由约束完成。