Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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 Swift:根据内部UICollectionView的大小展开UITableViewCell height_Ios_Swift_Uitableview_Uicollectionview_Cell - Fatal编程技术网

Ios Swift:根据内部UICollectionView的大小展开UITableViewCell height

Ios Swift:根据内部UICollectionView的大小展开UITableViewCell height,ios,swift,uitableview,uicollectionview,cell,Ios,Swift,Uitableview,Uicollectionview,Cell,大家好。我在一个月前开始学习编程,所以如果我不能很好地解释我的问题,请友好一些:) 我的项目由一个主UITableView组成。在UITableView的每个单元格中,我都有一个UICollectionView(水平滚动) 每个UICollectionViewCell的宽度与整个UITableViewCell相同。我的第一个问题是调整UITableViewCell的高度(这将取决于UICollectionView本身的大小以及顶部内容的大小) 这必须自动完成。事实上,UICollection

大家好。我在一个月前开始学习编程,所以如果我不能很好地解释我的问题,请友好一些:)

我的项目由一个主
UITableView
组成。在
UITableView
的每个单元格中,我都有一个
UICollectionView
(水平滚动)

每个
UICollectionViewCell
宽度与整个
UITableViewCell
相同。我的第一个问题是调整
UITableViewCell
的高度(这将取决于
UICollectionView
本身的大小以及顶部内容的大小)

这必须自动完成。事实上,
UICollectionViewCell
s将没有相同的高度,因此当用户水平滚动
UICollectionView
时,将出现一个新的
UICollectionViewCell
(高度不同),并且
UITableViewCell
的高度必须调整

我遇到的第二个问题是关于调整
UICollectionViewCell
的大小,事实上我事先不知道它的高度(宽度与
UITableView
相同)。我应该从nib导入内容

这是我的ViewController文件

变量包括:

@IBOutlet weak var tableView: UITableView!
var storedOffsets = [Int: CGFloat]()
UITableView
扩展:创建
UITableView
的单元格,并在其中设置
UICollectionView
的委托

extension IndexVC: UITableViewDelegate, UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 6 //Temp
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCellWithIdentifier("CellCollectionView") as? CellPost {
        let post = self.posts[indexPath.row]
        cell.configureCell(post)

        return cell
    } else {
        return CellPost()
    }

}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    guard let tableViewCell = cell as? CellPost else { return }

    tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
    tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
}

func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    guard let tableViewCell = cell as? CellPost else { return }

    storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
}
UICollectionView
部分:将视图从Nib/xib添加到单元格中

extension IndexVC: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 9 //Temp
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellInCollectionView", forIndexPath: indexPath)

    if let textPostView = NSBundle.mainBundle().loadNibNamed("textPostView", owner: self, options: nil).first as? textPostView {
        textPostView.configurePost(post.descriptionLbl)
        cell.addSubview(textPostView)
        textPostView.translatesAutoresizingMaskIntoConstraints = false
        cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":textPostView]))
        cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":textPostView]))

    }

    return cell
}
使单元格的大小与整个
UICollectionView

extension IndexVC: UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake(collectionView.frame.width, collectionView.frame.height)
}
我在专用于
UITableViewCell
的类中创建了这个扩展(对于这个问题没有用处,但如果你们想重新创建它的话)

extensioncellpost{
func setCollectionViewDataSourceDelegate(dataSourceDelegate:D,forRow row:Int){
collectionView.delegate=dataSourceDelegate
collectionView.dataSource=dataSourceDelegate
collectionView.tag=行
collectionView.setContentOffset(collectionView.contentOffset,动画:false)//如果正在滚动,则停止集合视图。
collectionView.reloadData()
}
var collectionViewOffset:CGFloat{
设置{
collectionView.contentOffset.x=newValue
}
得到{
返回collectionView.contentOffset.x
}
}
如果有人想使用此代码,它工作得很好,但我必须使用
表视图(…heightForRowAtIndexPath…)对
UITableView
的高度进行硬编码

我尽一切努力使
UITableViewCell
适应其中的内容(我尝试计算我放入单元格的Nib发送的内容大小,然后将其发送到
tableView(…heightforrowatinexpath…)
但我不能让它工作。我也尝试了自动布局,但可能我做错了。我还认为问题可能来自我在手机中导入笔尖的部分


我也不知道在用户刷过
UICollectionViewCell
后如何扩展单元格,有没有办法在这种情况下创建事件?或者调用
tableView(…HeightforRowatineXpath…)
再次?

据我所知,您需要自动调整tableview单元格高度。因此,您可以使用自动布局来调整单元格高度

在ViewDidLoad中写入

self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
返回CellForRowAtIndexPath中的单元格之前

self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
您可以找到自动布局的链接。
为了解决我的问题,我创建了一个数组来存储
UITableView
(名为
cellHeight
)中每个单元格的高度

我还计算由数组(名为
cellHeightForPost
)组成的数组中程序开始时每个单元格的高度。主数组表示所有
TableView单元格
,其中的每个数组表示“collectionView”中每个单元格的高度

为了每次更改表视图中的collectionView时都更新表视图,我使用了Function
collectionView(…willDiplayCell…

我的UITableView大小
tableView(…RowatineXpath高度)
tableView(…估计RowatineXpath高度)
中定义:

要设置
集合视图单元格的大小

return CGSize(width: collectionView.frame.width, height: cellHeightForPost[collectionView.tag][indexPath.row])

很少以这种方式使用它,当您决定使用UICollectionView时,在大多数情况下,我认为没有必要使用UITableView来包含它。@Jean Baptiste,公认的答案解决了您的问题吗?我的操作与您的代码完全相同,但当我使用UITableViewAutomaticDimension时,我的集合视图根本不绑定。您能给我一个提示吗请不要?
self.tableView.beginUpdates()
cellHeight[collectionView.tag] = cellHeightForPost[collectionView.tag][indexPath.row]
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
self.tableView.endUpdates()
return cellHeight[indexPath.row]
return CGSize(width: collectionView.frame.width, height: cellHeightForPost[collectionView.tag][indexPath.row])