Swift:根据其中的UICollectionView的大小扩展UITableViewCell高度

Jea*_*ste 5 cell uitableview ios uicollectionview swift

嗨,大家好.我1个月前开始学习编程,所以如果我不能很好地解释我的问题,请保持良好:)

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

图1:主视图

每个的宽度UICollectionViewCell与整个宽度相同UITableViewCell.我的第一个问题是关于尺寸的大小UITableViewCell(这取决于UICollectionView它自身的大小和它上面的内容的大小).

图2:CollectionView

这必须自动完成.事实上,UICollectionViewCells将不会具有相同的高度,因此当用户将UICollectionView水平滚动时,UICollectionViewCell将出现一个新的(具有不同的高度)并且UITableViewCell将必须适应高度.

我遇到的第二个问题是关于尺寸调整UICollectionViewCell,事实上我事先并不知道它的高度是什么(宽度与之相同UITableView).我应该从笔尖导入内容.

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

变量:

@IBOutlet weak var tableView: UITableView!
var storedOffsets = [Int: CGFloat]()
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

部分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
}
Run Code Online (Sandbox Code Playgroud)

使单元格的大小与整个单元格相同 UICollectionView

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

我在专门用于该类的类中创建了这个扩展UITableViewCell(对问题没用,但是如果有人想要重新创建它)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

如果有人想使用这个代码,它工作得很好,但我必须UITableView用a 来硬编码高度tableView( ... heightForRowAtIndexPath ...)

我想尽一切办法使UITableViewCell适应它里面有什么(我试过计算由我把在小区笔尖发送内容的大小,然后将其发送到tableView( ... heightForRowAtIndexPath ...) ,但我不能使它工作.我也尝试过自动布局但也许我做错了.我也认为这个问题可能来自于我在我的手机中导入笔尖的部分.

在用户刷过之后我也不知道扩展单元格的方法UICollectionViewCell,有没有办法在发生这种情况时创建事件?也许再打个电话tableView( ... heightForRowAtIndexPath ...)

小智 1

据我了解,您需要自动调整表格视图单元格高度。所以你可以使用自动布局来调整单元格高度。

写入ViewDidLoad

self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)

返回 CellForRowAtIndexPath 中的单元格之前

self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
Run Code Online (Sandbox Code Playgroud)

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