0nd*_*re_ 24 tableview ios uicollectionview swift
我有一个,tableView而且每个tableViewCell都是一个collectionView.
我试图用这段代码更改collectionView大小:
 func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    if collectionView.tag == 2{
        return CGSize(width: 100, height: 100)
    }else if collectionView.tag == 4 {
        return CGSize(width: 222, height: 200)
    }else{
        return CGSize(width: 10, height: 10)
    }
}
问题是没有错误,但细胞不会改变大小
如果您需要了解更多信息,请撰写评论.
谢谢.
Ped*_*eno 59
此方法的签名已在Swift 3中更改为:
func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    // your code here
}
注意细微差别:(_ collectionView: ...而不是(collectionView: ....
这是因为在Swift 3中,您必须明确指定第一个参数的标签.您可以通过添加下划线字符来覆盖此默认行为_.
此外,请确保您的类同时采用UICollectionViewDelegate和UICollectionViewDelegateFlowLayout协议
class MyViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
    // your code here
    func collectionView(_ collectionView: UICollectionView,
                layout collectionViewLayout: UICollectionViewLayout,
                sizeForItemAt indexPath: IndexPath) -> CGSize {
        // your code here
    }
}
请参阅此链接以了解有关Swift 3中的更改的更多信息.
小智 19
您需要指定在类声明中实现协议UICollectionViewDelegateFlowLayout.
class PhrasesCompactCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout
Bur*_*lek 11
首先,不要忘记从UICollectionViewDelegateFlowLayout委托进行扩展。
对于单个collectionview显示逻辑:
 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = ((collectionView.frame.width - 15) / 2) // 15 because of paddings
    print("cell width : \(width)")
    return CGSize(width: width, height: 200)
}
情节提要中的重点不要忘记估计大小:无
所以这是我现在的代码并且它有效:
        collectionView.delegate = dataSourceDelegate
        collectionView.dataSource = dataSourceDelegate
        collectionView.tag = row
        collectionView.setContentOffset(collectionView.contentOffset, animated:false)
        collectionView.reloadData()
        collectionView.register(UINib(nibName: "eventsCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "eventsCollectionViewCell")
        if row == 2{
            let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
            layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            layout.itemSize = CGSize(width: 120, height: 120)
            layout.scrollDirection = .horizontal
            collectionView.collectionViewLayout = layout
        }else if row == 4 {
            let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
            layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            layout.itemSize = CGSize(width: 222, height: 200)
            layout.scrollDirection = .horizontal
            collectionView.collectionViewLayout = layout
        }else{
            print("else")
        }
| 归档时间: | 
 | 
| 查看次数: | 39864 次 | 
| 最近记录: |