[英]Swift: Different number of cells in a row of a UICollectionView iOS
我想將UICollectionView的布局流程混合到垂直和水平,我想讓每一行都可以被2整除一個項目,否則它應該有一個兩列網格,我已經用盡了我能想到的每一種方法,我每行得到一件物品。
下面的圖片正是我想要實現的。
這是我試過的
class TrendVC: UIViewController {
fileprivate let itemsPerRow: CGFloat = 2
fileprivate let sectionInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
@IBOutlet weak var collectionView: UICollectionView!
var data = [ "others", "sports", "kitchen", "phones", "entertainment", "electronics", "women"]
var dataNames = ["Bags", "Sports & Outdoor", "Kitchen Essentials", "Mobile Phones & Tablets", "Entertainment", "Audio and Video", "Women's Clothings"]
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
collectionView.dataSource = self
collectionView.delegate = self
collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
}
extension TrendVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item % 3 == 0{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DoubleCell", for: indexPath) as! DoubleTrendCellCell
let datum = data[indexPath.item]
let dat = dataNames[indexPath.item]
cell.trendNameLabel.text = dat
cell.trendImageView.image = UIImage(named: datum)
return cell
}else{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrendCell", for: indexPath) as! SingleTrendCell
let datum = data[indexPath.item]
let dat = dataNames[indexPath.item]
cell.trendNameLabel.text = dat
cell.trendImageView.image = UIImage(named: datum)
return cell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.item % 3 == 0{
let paddingSpace = sectionInsets.left * (2 + 1)
let availableWidth = view.frame.width - paddingSpace
let widthPerItem = availableWidth / itemsPerRow
return CGSize(width: widthPerItem, height: widthPerItem)
}
let paddingSpace = sectionInsets.left * (2 + 1)
let availableWidth = view.frame.width - paddingSpace
let widthPerItem = availableWidth / itemsPerRow
return CGSize(width: view.frame.width, height: widthPerItem)
}
//3
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return sectionInsets
}
// 4
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return sectionInsets.left
}
}
這就是我得到的
由於您的兩個單元格具有相同的結構,因此創建兩種不同類型的單元格並不重要。 您所要做的就是使用sizeForItemAtIndexPath
方法並返回適當的大小, autoLayout
將負責內部的內容。 就像是:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.item % 3 == 0{
return CGSize(width: collectionView.bounds.size.width/2.0, height: collectionView.bounds.size.height/3.0)
}else{
return CGSize(width: collectionView.bounds.size.width, height: collectionView.bounds.size.height/3.0)
}
}
注意:確保所有插入和項間距均為0。
您需要使用3
而不是2
來計算模數以獲得所需的輸出。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item % 3 == 0{
//return SingleTrendCell
}else{
//return DoubleTrendCellCell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.item % 3 == 0{
//return SingleTrendCell Size
}
//return DoubleTrendCellCell Size
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.