Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios tableview';s的行高度不变,CollectionView将捕获所有未使用的空间,而不是在tableViewCell中调整自身大小_Ios_Swift2_Uicollectionview_Uicollectionviewlayout_Ios Autolayout - Fatal编程技术网

Ios tableview';s的行高度不变,CollectionView将捕获所有未使用的空间,而不是在tableViewCell中调整自身大小

Ios tableview';s的行高度不变,CollectionView将捕获所有未使用的空间,而不是在tableViewCell中调整自身大小,ios,swift2,uicollectionview,uicollectionviewlayout,ios-autolayout,Ios,Swift2,Uicollectionview,Uicollectionviewlayout,Ios Autolayout,我将UICollectionView放入UITableViewCell,然后在我的UICollectionViewCell中有一个图像视图。因此,当我运行我的应用程序时,集合视图不会同时调整自身大小。在我的单元格中,我放置了一个文本视图,该视图会根据内容调整自身大小,请参见下图: 在第一幅图像中,我在顶部有一个文本视图,其中包含一些文本,在它下面有(粉色背景)的集合视图,在绿色背景的集合视图中有我的图像视图,正如您所看到的,集合视图占用了额外的空间,而不是像文本视图那样减少了自身的空间。 在第

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

在第一幅图像中,我在顶部有一个文本视图,其中包含一些文本,在它下面有(粉色背景)的集合视图,在绿色背景的集合视图中有我的图像视图,正如您所看到的,集合视图占用了额外的空间,而不是像文本视图那样减少了自身的空间。

在第二幅图中,您可以看到我的文本视图比以前有更多的内容,因此它的大小现在与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

        }
    }


}
这是我的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
    }    

}
如何根据收藏视图中的内容将其设置为
AutoLayout
本身?我也试过:

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

但是不起作用(我的收藏视图消失了)如果有人知道怎么做,请指导我

在表视图单元格中使用集合视图时,我遇到了类似的问题。无论我做了什么,我都无法让表视图自动调整大小,但集合视图却做到了。我用代码来代替自动布局

最后,我不得不在collection视图中计算集合视图numberOfSections中标签的大小,并使用委托将此高度传递给具有tableView委托和数据源的视图控制器,然后重新加载表视图的相应行

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

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

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

    return 1;
}
这应该给你一个大概的想法

编辑:对不起,我误解了你之前的问题。这里有一些东西应该适合你:

根据我的理解,您有一个表视图单元格,其中包含标签视图和集合视图

现在,在表视图单元格中,应该向标签添加顶部、前导和尾随约束空间。现在,在集合视图中,将图像垂直放置在中心,并在单元格superview中添加适当的顶部和底部。集合视图本身在前导、尾随、从上到标签和从下到超级视图中应该有一个常量值。还可以向集合视图添加固定高度约束(假设希望图像大小保持不变)

现在让我们说视图控制器A是表视图的数据源,而表视图单元格是集合视图的数据源

在viewController A中,应将行的高度写为-

 - (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为该特定索引添加标签的高度,其余部分应使用约束。

如果Collectionview调整大小有效,则tableview单元格应自行调整大小,以防放置正确的布局。Collectionview不会自动调整自身大小,存在内部布局system@Anton你的意思是我必须单独处理collectionView的布局??是的。但将任何对象的laoyut放在tableview单元格中,这样自动demsion应该调整单元格的大小first@Anton你是什么意思?请详细说明一下好吗?UITableViewAutomaticDimension不能用于错误的单元格布局。现在不是调整tebleview手机的大小。伙计,这个collectionView的事情真是让人头疼,我希望你的解决方案能对我起作用,你能再详细说明一下吗?/hey@IrLearn请看我的这个问题@remyboys,是的,确定是哪一部分?比如如何添加约束,或者计算高度,或者全部?我会适当地修改我的答案。(否则这将是一个很大的评论)至于另一个问题,我不认为流布局是导致崩溃的原因。根据您的错误消息,它应该会显示一个奇怪的UI,但我认为它不应该崩溃。我以前从未使用过UICollectionView,也不太了解布局api,因此,如果您能提供一个简短的演示或我试图实现的内容片段,那么它将对我有好处@IeLearnThere,编辑评论。那应该对你有用。
 - (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.
}