Swift:根据其中的UICollectionView的大小扩展UITableViewCell的高度 [英] Swift: Expand UITableViewCell height depending on the size of the UICollectionView inside it

查看:95
本文介绍了Swift:根据其中的UICollectionView的大小扩展UITableViewCell的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

My project is composed of a main UITableView. In each cell of the UITableView, I have a UICollectionView (on horizontal scrolling).

图1:主视图

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

The width of each UICollectionViewCell is the same as the entire UITableViewCell. My first problem is about sizing the height of the UITableViewCell (which will depend of the size of the UICollectionView itself and the size of the content on top of it).

图2:CollectionView

此操作必须自动完成。实际上, UICollectionViewCell s 不会具有相同的高度,因此当用户滚动 UICollectionView 水平,将出现一个新的 UICollectionViewCell (具有不同的高度),并且 UITableViewCell 的高度将具有

This has to be done automatically. In fact, the UICollectionViewCells will not have the same height, so when the user will scroll the UICollectionView horizontally, a new UICollectionViewCell will appear (with different height) and the height of the UITableViewCell will have to adapt.

我遇到的第二个问题是确定 UICollectionViewCell 的大小,实际上我不会事先知道它的高度将是多少(宽度与 UITableView 相同)。我应该从笔尖导入内容。

The second problem I have is about sizing the UICollectionViewCell, in fact I will not know in advance what the height of it is going to be (the width is the same as the UITableView). I should import the content from a nib.

所以现在这是我的ViewController文件,

So now here is my ViewController file,

变量:

@IBOutlet weak var tableView: UITableView!
var storedOffsets = [Int: CGFloat]()

UITableView 扩展:创建 UITableView 的单元格,并在其中设置 UICollectionView 的委托

the UITableView extension : Create the cell of the UITableView and set delegate of UICollectionView inside it

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中的视图添加到单元格中

the part of the UICollectionView : Add the view from a Nib/xib to the cell

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

Make the size of the cell the same as the entire UICollectionView

extension IndexVC: UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake(collectionView.frame.width, collectionView.frame.height)
}

然后我在 UITableViewCell 专用的类中创建了该扩展名(对问题没有用,但是如果您要重新创建它)

And I created this extension in the class dedicated for the UITableViewCell (not useful for the problem, but if any of you want to recreate it)

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

    collectionView.delegate = dataSourceDelegate
    collectionView.dataSource = dataSourceDelegate
    collectionView.tag = row
    collectionView.setContentOffset(collectionView.contentOffset, animated:false) // Stops collection view if it was scrolling.
    collectionView.reloadData()
}

var collectionViewOffset: CGFloat {
    set {
        collectionView.contentOffset.x = newValue
    }

    get {
        return collectionView.contentOffset.x
    }
}

如果有人想使用此代码,效果很好,但是我必须用 UITableView 的高度> tableView(... heightForRowAtIndexPath ...)

If anyone want to use this code, it works great, but I have to hardcode the height of the UITableView with a tableView( ... heightForRowAtIndexPath ...)

我尽一切努力使 UITableViewCell 适应其中的内容(我尝试计算我放入单元格中的笔尖发送的内容的大小,然后将其发送到 tableView(... heightForRowAtIndexPath ...)
但我无法使其工作。我也尝试过自动布局,但也许我做错了。我也认为问题可能出在我将笔尖导入我的部分中

I tried everything to make the UITableViewCell adapt to what's inside it (I tried calculating the size of content sent by the Nib I'm putting in the cell, and then send it to tableView( ... heightForRowAtIndexPath ...) but I can't make it work. I also tried Auto Layouts but maybe I'm doing it wrong. I also think the problem could come from the part that I imported the nib in my cell.

我也不知道在用户刷卡后扩展单元格的方法ed UICollectionViewCell ,有什么办法可以在事件发生时创建事件?也许再次调用 tableView(... heightForRowAtIndexPath ...)

I also have no idea of a way to expand the cell after the user has swiped the UICollectionViewCell, is there any way to create an event when that happens? And maybe call tableView( ... heightForRowAtIndexPath ...) again?

推荐答案

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

As i understanding you need to auto adjust tableview cell height. so you can use autolayout for adjust cell height.

在ViewDidLoad中写入

Write in ViewDidLoad

self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension

之前CellForRowAtIndexPath中的返回单元格

Before Returning cell in CellForRowAtIndexPath

self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()

您可以找到自动布局的链接。
在此处输入链接描述

You can find link for autolayout. enter link description here

这篇关于Swift:根据其中的UICollectionView的大小扩展UITableViewCell的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆