【问题标题】:Is completely static UICollectionView possible?完全静态的 UICollectionView 可能吗?
【发布时间】:2015-02-15 09:23:55
【问题描述】:

在 UITableView,完全静态的 tableView 配置是可能的。您可以使用 IB 断开 UITableView 的数据源并将每个单元格放在情节提要(或 xib)上。

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

没有数据源的 UICollectionView 可以吗?

【问题讨论】:

  • 当您在cellForItemAtIndexPath 中构建单元格时,您可以访问静态单元格数组,而不是使用dequeueReusableCellWithReuseIdentifier。我只会在你有一个非常强大的用例时才这样做,因为它更有可能导致错误和性能问题。

标签: ios objective-c iphone uicollectionview


【解决方案1】:

没有。

不允许创建静态UICollectionViewController。您必须有一个数据源委托。

我还想指出,不是静态的UITableView,而是静态的UITableViewController。这是不同的。

【讨论】:

  • 好的。但你的依据是什么?苹果文件?你得到了 Apple UIKit 设计师的回应吗?还是你的经历?
  • 经验。我没有查,但你可以在 IB 中看到,与 UITableViewController 不同,UICollection VC 没有给你使用静态集合的选项。您还可以看到,在集合视图中添加更多单元格只是添加单元格格式。
【解决方案2】:

您可以轻松创建静态 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]
    }
}

然后将视图控制器设置为适当的类,并且,嘿,预先设置为(基本上)静态集合。很抱歉,但当您拥有一组控件时,这是支持纵向和横向视图的最佳方式……

【讨论】:

    【解决方案3】:

    我做了一些试验并想添加自己的方法,因为它帮助我实现了我正在寻找的真正静态的、高度自定义的集合视图。

    您可以为要在集合视图中显示的每个单元格创建自定义UICollectionViewCells,并使用集合视图中的所有单元格 ID 注册它们,如下所示:

    创建你的静态单元格:

    class MyRedCell: UICollectionViewCell {
        override init(frame: CGRect) {
            super.init(frame: frame)
            contentView.backgroundColor = .red
        }
    
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    

    根据需要制作尽可能多的这些。

    然后回到你的 Collection View Controller,用它们对应的 cellId 注册它们:

    let cellIds = ["redCell","blueCell","greenCell"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.register(MyRedCell.self, forCellWithReuseIdentifier: "redCell")
        collectionView.register(MyBlueCell.self, forCellWithReuseIdentifier: "blueCell")
        collectionView.register(MyGreenCell.self, forCellWithReuseIdentifier: "greenCell")
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIds[indexPath.item], for: indexPath)
    
        return cell
    }
    

    每个单元格将准确显示其类中的内容。

    【讨论】:

      猜你喜欢
      • 2016-04-28
      • 2021-11-21
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      相关资源
      最近更新 更多