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

2023-12-26

大家好。我 1 个月前开始学习编程,所以如果我没有很好地解释我的问题,请友善:)

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

图 1:主视图 https://i.stack.imgur.com/QKZqp.png

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

图 2:集合视图 https://i.stack.imgur.com/lF5sn.png

这必须自动完成。事实上,UICollectionViewCells will 高度不一样,所以当用户滚动时UICollectionView水平方向上,一个新的UICollectionViewCell将出现(具有不同的高度)并且高度UITableViewCell将不得不适应。

我遇到的第二个问题是关于调整大小UICollectionViewCell,事实上我不会提前知道它的高度是多少(宽度与UITableView)。我应该从笔尖导入内容。

现在这是我的 ViewController 文件,

变量:

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

the 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(对于该问题没有用,但如果你们中的任何人想重新创建它)

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 with a tableView( ... heightForRowAtIndexPath ...)

我尝试了一切来制作UITableViewCell适应其中的内容(我尝试计算我放入单元格中的笔尖发送的内容的大小,然后将其发送到tableView( ... heightForRowAtIndexPath ...)但我无法让它发挥作用。我也尝试过自动布局,但也许我做错了。我还认为问题可能来自于我在手机中导入笔尖的部分。

我也不知道在用户滑动后如何展开单元格UICollectionViewCell,有什么办法可以在发生这种情况时创建一个事件吗?也许打电话tableView( ... heightForRowAtIndexPath ...) again?


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

写入ViewDidLoad

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

返回 CellForRowAtIndexPath 中的单元格之前

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

您可以找到自动布局的链接。在此输入链接描述 http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swift:根据其中 UICollectionView 的大小扩展 UITableViewCell 高度 的相关文章

  • Swift 3 错误:[_SwiftValue pointSize] 无法识别的选择器发送到实例

    我刚刚将我们的项目迁移到 swift 3 发现由于一个问题导致大量崩溃 由于未捕获的异常 NSInvalidArgumentException 而终止应用程序 原因 SwiftValue pointSize 发送到实例的无法识别的选择器 该
  • 自动布局、UIDynamics 和动画

    我对自动布局还很陌生 并且对如何为视图设置动画感到困惑 我读了很多 我知道你必须遵守限制 编辑它 然后包装layoutIfNeeded in an UIView动画块 但当真正要做的时候 我却有点失落 我很乐意有人能向我解释如何做这个动画
  • 暂停视频录制[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在尝试创建一个应用程序 用户可以在其中从相机录制视频 该应用程序的功能之一必须是文件录制中的暂停 例如 用户通过按 开始 按钮开始
  • 如何检测Retina高清显示屏?

    UIScreen有一个新的 nativeScaleiOS 8 中的属性 但文档没有提及它 property nonatomic readonly CGFloat nativeScale 还有一个scale属性 但文档说它是 2 用于视网膜显
  • 我们可以从 LinkPresentation 框架中的 LPLinkView 中提取图像吗?

    我想在我的应用程序中呈现丰富的链接 并将这些数据发送到我的服务器 我需要访问视图内的图像LPLinkView https developer apple com documentation linkpresentation lplinkvi
  • 在另一种语言中使用 dateFormatter [重复]

    这个问题在这里已经有答案了 我正在运行一段返回的代码nil在具有不同语言设置的 iPhone 上运行时 代码示例如下所示 let dateFormatter DateFormatter dateFormatter dateFormat MM
  • iOS 8 中的 UISplitViewController 状态恢复

    在 iOS 8 上 UISplitViewController 似乎可以保存和恢复其子视图的状态 例如 主视图是否隐藏 这是不可取的 因为我的应用程序应该始终以横向方式显示主视图 并始终以纵向方式隐藏它 如果用户以横向模式关闭应用程序 保存
  • PresentModalViewController 不执行任何操作

    我有一个 UIViewController parent 调用presentModalViewController与另一个 UIViewController child on viewDidLoad If parent没有 UINaviga
  • 在后台任务中安排通知

    我正在为 iOS 开发一个日历 闹钟应用程序 它与网络服务器同步 当在服务器上添加活动时 会发出推送通知 以便 iOS 客户端可以获取新数据 并根据需要更新和安排下一次警报的时间 本地通知 但这仅在应用程序在客户端打开时才有效 我希望客户端
  • 在 Swift 中自动移动 UISlider

    我想在按下按钮时将 UISlider 从 minValue 循环移动到 maxValue 并在再次按下按钮时将其停止在当前位置 我想使用 Swift 我遇到的主要问题是函数 slider setValue 太快了 我希望动画更慢 IBAct
  • 线程 1:信号 SIGABRT - AppDelegate.h

    main m Journey Created by Julian Buscema on 2014 07 13 Copyright c 2014 Julian Buscema All rights reserved import
  • FireMonkey iOS RAD Studio XE2 - 在从 URL 加载的表单上显示图像

    是否可以将 TImage 放置在 iOS 的 FMX 表单上 并将图像 jpg 从 URL 加载到此 TImage 中以在 iOS 应用程序中显示 我尝试过但没有成功 任何正确方向的提示或指出都会受到赞赏 将 TButton TImageC
  • Objective C UIImagePNGRepresentation内存问题(使用ARC)

    我有一个基于 ARC 的应用程序 它从 Web 服务加载大约 2 000 个相当大 1 4MB 的 Base64 编码图像 它将 Base64 解码后的字符串转换为 png图像文件并将其保存到磁盘 这一切都是在一个循环中完成的 我不应该有任
  • 应用程序传输安全已禁用,但仍然出现 SSL 握手错误

    我在通过 HTTPS SSL 连接到 API 时遇到问题 我已经使用下面的字典完全禁用了应用程序传输安全性 ATS 尽管 SSL 证书通过了 NSCURL 的所有测试
  • 如何将音乐从我的应用程序切换到 iPod

    我在用MusicPlayerController我的应用程序中的对象来播放音乐 我知道当 iPhone ipod 应用程序终止时 可以继续播放我的应用程序音乐 我该怎么做 这涉及到一些事情 您必须在两种音乐播放器之间进行选择 应用程序音乐播
  • 对使用phonegap和钛的质疑[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 最近我听说了 PhoneGap 和 Titanium 移动网络应用程序的开发 我分析了这两个 Web 应用程序 并了解了如何使用它们以
  • Xcode 9 中的“addingPercentEncoding”是否损坏?

    在 Swift 3 x 和 Xcode 9 beta 2 中 使用addingPercentEncoding https developer apple com documentation swift string 1690785 addi
  • 自定义 MKAnnotationView - 如何捕获触摸而不忽略标注?

    我有一个自定义 MKAnnotationView 子类 它完全按照我想要的方式显示视图 在那个视图中 我有一个按钮 我想捕获按钮上的事件来执行操作 这很好用 但是 我不希望标注被忽略或消失 基本上 触摸标注中的按钮将开始播放声音 但我想保留
  • 如何获取 UIWebView 中元素的位置?

    我在 iPad 程序中加载了 html 的 UIWebView 通过使用 webkit column width 我将 html 分为几列 padding 0px height 1024px webkit column gap 0px we
  • ResponseSerializer“无法使用 Swift 3 调用非函数类型“NSHTTPURLResponse”的值?

    我一直在使用以下代码 没有出现任何问题 直到更新到 Xcode 8 beta 6 它类似于这个例子 https github com Alamofire Alamofire generic response object serializa

随机推荐