在UITableView中,完全静态的tableView配置是可能的。您可以断开UITableView的数据源的连接,并使用IB将每个单元格放在故事板(或xib)上。
我也试过用UICollectionView做同样的事情。断开UICollectionView的数据源。将每个单元格放在故事板上的UICollectionView上。我在没有任何错误的情况下构建它。但它不起作用。根本没有显示单元格。
没有数据源的UICollectionView是可能的吗?
发布于 2015-02-16 13:42:20
不是的。
不允许创建静态UICollectionViewController
。您必须有一个数据源委托。
我还想指出的是,这里没有静态UITableView
,而是静态UITableViewController
。这是不同的。
发布于 2016-09-15 05:31:11
您可以轻松地创建静态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]
}
}
然后将视图控制器设置为适当的类,并且(基本上)设置为静态集合。我很抱歉地说,当你有一组控件时,这是支持纵向和横向视图的最好方式……
发布于 2020-02-02 16:49:10
我做了一些实验,并想添加我自己的方法,因为它帮助我实现了我正在寻找的真正静态的、高度自定义的集合视图。
您可以为要在集合视图中显示的每个单元格创建自定义UICollectionViewCell
,并使用集合视图中的所有单元格in注册它们,如下所示:
创建静态单元格:
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")
}
}
你想做多少就做多少。
然后在你的集合视图控制器中,用它们对应的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
}
每个单元格将准确地显示其类中的内容。
https://stackoverflow.com/questions/28524691
复制相似问题