【问题标题】:sizeForItemAt not called while scrolling滚动时未调用 sizeForItemAt
【发布时间】:2019-12-17 05:46:29
【问题描述】:

我在UITableViewCell 中有UICollectionView,它的委托和数据源在表格单元格类中声明。它在第一次加载时工作正常,但后来在我滚动收藏时改变了它的大小。 我注意到sizeForItemAt 仅在加载时调用,而不是在滚动期间设置单元格时调用。

class HomeCell: UITableViewCell {

    @IBOutlet weak var collHomeSongs: UICollectionView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func setCollection() {
        collHomeSongs.delegate = self
        collHomeSongs.dataSource = self
    }
}

extension HomeCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: 120, height: 180)
    }
}

【问题讨论】:

  • 发布您已经尝试过的代码。
  • 你还没有调用 setCollection() 请在 viewDidload() 中调用

标签: ios swift uitableview uicollectionview


【解决方案1】:

我刚刚得到它的解决方案这个问题出现在 Xcode 11+ 中,解决方案是将集合视图自动大小设置为“无”。 参考:Why on Xcode 11, UICollectionViewCell changes size as soon as you scroll (I already set size in sizeForItem AtIndexPath:)?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多