首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >尽管实现了sizeForItemAt,但UICollectionViewFlowLayout单元格大小始终不变

尽管实现了sizeForItemAt,但UICollectionViewFlowLayout单元格大小始终不变
EN

Stack Overflow用户
提问于 2017-12-20 04:43:26
回答 1查看 2.4K关注 0票数 6

我有一个UICollectionViewCell,里面有一个UICollectionView,里面有UICollectionViewFlowLayout。我将单元格作为集合视图的委托和数据源。我符合UICollectionViewDelegateFlowLayout并实现了collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize (并返回正确的大小)。

但是,当我调用layout.itemSize.height时,我得到的默认高度是50。

代码语言:javascript
复制
class MyCell: UICollectionViewCell {
  fileprivate lazy var collectionView: UICollectionView = {
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.register(SomeOtherCell.self, forCellWithReuseIdentifier: SomeOtherCell.reuseIdentifier)
    collectionView.showsHorizontalScrollIndicator = false
    collectionView.showsVerticalScrollIndicator = false
    return collectionView
  }()

  fileprivate lazy var layout: UICollectionViewFlowLayout? = {
    let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
    layout?.scrollDirection = .horizontal
    layout?.sectionInset = UIEdgeInsets.zero
    return layout
  }()

  override func sizeThatFits(_ size: CGSize) -> CGSize {
  collectionView.frame = CGRect(x: layoutMargins.left, y: origin.y, width: width, height: layout.itemSize.height)
  // layout.itemSize.height is always 50
  }
}
extension MyCell: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    guard tags.count != 0 else {
        return .zero
    }

    let tag = Tag(text: tags[indexPath.item], index: indexPath.item)
    placeholderCell.configure(with: tag)
    let size = placeholderCell.getFrame(at: .zero, fitting: placeholderCell.width, alignedBy: semanticContentAttribute, apply: false).size
    collectionViewWidth += size.width
  // this size is always correct, that's what I want when I call  layout.itemSize.height 
    return size
  }
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-05 19:26:02

我觉得你的注册问题...移除它,并在情节提要中设置该单元格。只是胡乱猜测..。

您有2个单元格,其中哪一个注册了collectionView?

如果您在情节提要中设置单元格,注册也可能会导致问题

代码语言:javascript
复制
  collectionView.register(SomeOtherCell.self, forCellWithReuseIdentifier: SomeOtherCell.reuseIdentifier)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47894895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档