完全静态的UICollectionView可能吗?

kmu*_*ani 26 iphone objective-c ios uicollectionview

在UITableView中,可以使用完全静态的tableView配置.您可以断开UITableView的数据源,并使用IB将每个单元格放在故事板(或xib)上.

我用UICollectionView尝试了同样的事情.断开UICollectionView的数据源.将每个单元格放在故事板上的UICollectionView上.我没有任何错误地构建它.但它没有奏效.根本没有显示细胞.

没有数据源的UICollectionView可能吗?

小智 18

没有.

UICollectionViewController不允许创建静态.您必须具有数据源委托.

我还想指出,没有静态UITableView,而是静态UITableViewController.这是一个区别.


Dan*_*aan 15

您可以轻松创建静态UICollectionViewController.

只需在界面构建器中创建每个单元格,给它们重复使用标识符(例如"Home_1""Home_2""Home_3"),并按如下方式填充方法:

class HomeViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {  
    let cellIdentifiers:[String] = ["Home_1","Home_2","Home_3"]
    let sizes:[CGSize] = [CGSize(width:320, height:260),CGSize(width:320, height:160),CGSize(width:320, height:100)]

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cellIdentifiers.count
    }
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifiers[indexPath.item], for: indexPath)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return sizes[indexPath.item]
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将视图控制器设置为正确的类,并且,嘿presto,一个(基本上)静态集合.我很遗憾地说,当你有一组控件时,这是支持肖像和风景视图的最佳方式.