- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的 UICollectionView 中有一个无限滚动,我注意到我估计单元格高度的方式是我的 Collection View 的瓶颈。当我滚动我的 Collection View 时,它会导致一些长时间的延迟。
是否有更好的方法来估算我的单元格的高度?
单元格具有不同的高度,因为我在每个单元格中都有一个 UILabel
。我为这些 UILabel 分配了一个不同长度的 NSMutableAttributedStrings:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .justified
paragraphStyle.lineSpacing = 5.0
let attributedText = NSMutableAttributedString(string: " \(post.caption)", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15), .paragraphStyle: paragraphStyle, .baselineOffset: NSNumber(value: 0)])
attributedText.append(NSAttributedString(string: "\n\n", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 4)]))
let timeAgoDisplay = post.creationDate.timeAgoDisplay()
attributedText.append(NSAttributedString(string: timeAgoDisplay, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14), NSAttributedStringKey.foregroundColor: UIColor.storiesLightGray()]))
captionLabel.attributedText = attributedText
我的sizeForItemAt
方法。我注意到,一旦我的 Collection View 中有 200 多个项目,调用 dummyCell.layoutIfNeeded()
会使我的应用程序变得非常慢:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var height: CGFloat = 180
let frame = CGRect(x: 0, y: 0, width: view.frame.width, height: height)
let dummyCell = HomePostCell(frame: frame)
dummyCell.post = presenter.posts[indexPath.item]
dummyCell.layoutIfNeeded()
let targetSize = CGSize(width: view.frame.width, height: 5000)
let estimatedSize = dummyCell.systemLayoutSizeFitting(targetSize)
let newHeight = max(height, estimatedSize.height)
return CGSize(width: view.frame.width, height: newHeight)
}
谢谢!
最佳答案
理想情况下,您不应在 sizeForItemAt
中使用 systemLayoutSizeFitting
,因为正如您所说,它很慢。
您可以预缓存一些单元格数据,计算大小,然后将它们存储在数组或类似的数组中,这样 sizeForItemAt
只需在数组中进行查找——速度很快。
而且您实际上也不需要使用 systemLayoutSizeFitting
;您可以使用您对标签尺寸和内容的了解来计算尺寸(例如,使用 NSAttributedString 测量方法)。
这就是我们在 systemLayoutSizeFitting
或自动布局存在之前所做的事情,而且速度仍然快得多。
关于ios - 在 CollectionView 中估计单元格高度的更快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50518955/
我在 Storyboard 中有一个 collectionView,如下所示: 注意约束:Collection View.top = Search Bar.bottom 我不想要该行上方的 Colle
我有一个包含两个部分的 Collection View ,每个部分都有一个项目。部分中的单元格包含 Collection View ,我需要使高度单元格适合内容 Collection View 。 我
我见过将 Collection View 嵌套在 TableView 中的解决方案,但对于我的应用程序,我需要有 2 个 Collection View ,因为这样可以更轻松地执行其他操作。 因此,让
感谢帮助。 如上所示,CollectionView ItemSize.height 比 CollectionView.height 大。我只是在 viewDidLayoutSubviews() 中设置
我有一个collectionview(A),它可以作为“卡片 View ”来滑动。在最后一个卡/单元格中,我有另一个 Collection View (B)来显示更多单元格。到目前为止,一切都很好!一
我正在寻找一种方法,当您滚动到 collectionView 中的另一个单元格时,可以更改 imageView 中的图像。 因此,当滚动单元格的 (1) 时,我想更改图像 (2)。 创建此内容的最佳方
我的 NSCollectionView 项目集成了另一个 NSCollectionView。一切都很好,但在第一个 View 中选择项目时就不行了。 当我设置collectionView1.isSel
我是一名 Android 开发者,正在尝试学习 iO 开发。 我正在尝试在水平分页滚动器内实现垂直滚动 UI(就像 ViewPager 内的 RecyclerView)。 我知道我应该使用嵌套的 UI
我在collectionView中有一个collectionView,我希望能够选择第二个collectionView中的一个单元格来执行到另一个ViewController的segue。 目前,当我
我有这两个代表: #pragma mark = UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView*)co
嘿,我正在尝试在我的动态 Collection View 单元格之后添加一个静态 Collection View 单元格。例如在我的屏幕截图中,在“Study Grind Edition” Colle
是否可以通过在 Collection View Cell .swift 文件中编写代码来阻止我的 Collection View 滚动。我希望能够在用户点击单元格中的按钮时停止滚动,然后在再次按下按钮
我有一个用例,我想要呈现一组collectionViews(或复合 View )。使用 marionette.js 执行此操作的最佳方法是什么? 我的模型结构如下- A | |-- Collectio
我有一个 View Controller ,其中有 tableView 和 Collection View 。我为 collectionView 和 tableView 设置了相同的高度。 前半部分是
在 Visual Studio 2017 中,尝试存档 Xamarin Android 项目时,成功构建后出现以下错误: 无法创建应用程序存档“MyArchive”。这种类型的 CollectionV
我对 Collection View 完全陌生,请帮助我在 Collection View 中垂直和水平滚动单元格: viewcontroller.h #import @interface View
我在一个表格 View 单元格中有两个不同的 collectionView。原因是因为我试图找到实现一个水平 collectionView 和一个显示不同数据的垂直 collecitonView 的最
我想在另一个 collectionview 标题中实现一个 Collection View 幻灯片,但是当我尝试连接 uicollectionview 的导出时,我收到一条错误消息,指出导出无法连接到
您好,我已经为这个问题苦苦挣扎了一段时间,一直想不出来。 我想做的是,在 collectionview 单元格内按下一个按钮,然后执行到下一个单元格的转换。 这是我想在 collectionview
在 collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayo
我是一名优秀的程序员,十分优秀!