Ios 如何强制每行的项目数
我有一个集合视图,我想呈现以下布局 我试图限制单元格的数量,但我不确定如何强制每行不同的计数,正如您在设计中看到的那样Ios 如何强制每行的项目数,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我有一个集合视图,我想呈现以下布局 我试图限制单元格的数量,但我不确定如何强制每行不同的计数,正如您在设计中看到的那样 类ViewController:UIViewController{ 私有(集合)惰性变量collectionView:UICollectionView={ 让collectionView=UICollectionView(框架:view.frame,collectionViewLayout:UICollectionViewFlowLayout()) collectionVie
类ViewController:UIViewController{
私有(集合)惰性变量collectionView:UICollectionView={
让collectionView=UICollectionView(框架:view.frame,collectionViewLayout:UICollectionViewFlowLayout())
collectionView.autoresizingMask=[.flexibleHeight、.flexibleWidth]
collectionView.backgroundColor=.systemBackground
collectionView.dataSource=self
collectionView.register(UICollectionViewCell.self,forCellWithReuseIdentifier:“CustomCell”)
返回集合视图
}()
重写func viewDidLoad(){
super.viewDidLoad()
collectionView.backgroundColor=#colorLiteral(红色:0.1215686275,绿色:0.1294117647,蓝色:0.1490196078,alpha:1)
collectionView.contentInset=.init(顶部:24,左侧:24,底部:24,右侧:24)
view.addSubview(collectionView)
}
}
扩展视图控制器:UICollectionViewDataSource{
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回(5*5)+6
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
let cell=collectionView.dequeueReusableCell(带有reuseidentifier:“CustomCell”,用于:indexath)
cell.backgroundColor=#colorLiteral(红色:0.1647058824,绿色:0.1764705882,蓝色:0.2117647059,alpha:1)
cell.layer.cornerRadius=cell.frame.width/2
返回单元
}
}
我的尝试只会导致
颜色不仅对布局很重要。如果可以定义集合视图的高度/宽度,则可以计算位置
你的密码很好。。尽管您需要将UICollectionViewFlowLayout子类化,并将该子类传递到UICollectionView初始化中。布局如何成形的逻辑将在该子类中。不过我得问个简单的问题。。你能不能只显示/隐藏第1个和第X个单元格中的内容?谢谢Derek,我如何计算要隐藏的单元格?在大屏幕上,第7个单元格可能没有小屏幕的位置,因此我会隐藏错误的单元格。您需要知道单元格大小、填充和屏幕大小,才能知道哪个单元格最左上角和最右上角。最下面一行也一样。如果您是iOS 13及更高版本的用户,使用合成布局也很简单。
class ViewController: UIViewController {
private(set) lazy var collectionView: UICollectionView = {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.backgroundColor = .systemBackground
collectionView.dataSource = self
collectionView.delegate = self
collectionView.isScrollEnabled = false
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CustomCell")
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.backgroundColor = #colorLiteral(red: 0.1215686275, green: 0.1294117647, blue: 0.1490196078, alpha: 1)
collectionView.contentInset = .init(top: 24, left: 24, bottom: 24, right: 24)
view.addSubview(collectionView)
NSLayoutConstraint.activate([
collectionView.heightAnchor.constraint(equalToConstant: 340),
collectionView.widthAnchor.constraint(equalToConstant: 300),
collectionView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
collectionView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 48
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath)
switch indexPath.item {
case 0, 5, 42, 47: cell.backgroundColor = #colorLiteral(red: 0.1215686275, green: 0.1294117647, blue: 0.1490196078, alpha: 1)
default: cell.backgroundColor = #colorLiteral(red: 0.1647058824, green: 0.1764705882, blue: 0.2117647059, alpha: 1)
}
cell.layer.cornerRadius = cell.frame.width / 2
return cell
}
}
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return .init(width: 28, height: 28)
}
}