这是我在 swift 3 中的代码
class BSViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14","15","16"]
// MARK: - UICollectionViewDataSource protocol
// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
cell.myLabel.text = self.items[indexPath.item]
cell.backgroundColor = UIColor.cyan // make cell more visible in our example project
return cell
}
// MARK: - UICollectionViewDelegate protocol
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
if self.items.count % 3 > 0 {
print(self.items.count % 3)
return (self.items.count/3)+1
}
else {
return self.items.count/3
}
}
private func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
var collectionViewSize = collectionView.frame.size
collectionViewSize.width = collectionViewSize.width/3.0 //Display Three elements in a row.
collectionViewSize.height = collectionViewSize.height/4.0
return collectionViewSize
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "Footer", for: indexPath)
// configure footer view
return view
}
}
结果,它显示了 6 行,每行有 3 个单元格,并且必须是 6 行,每行包含 3 个单元格,期望最后一行必须有 2 个剩余单元格。
结果不正确
[ X X X ]
[ X X X ]
[ X X X ]
[ X X X ]
[ X X X ]
[ X X X ]
正确的结果
[ X X X ]
[ X X X ]
[ X X X ]
[ X X X ]
[ X X X ]
[X X]
Best Answer-推荐答案 strong>
这真的是一道数学题,Swift 没有错。
有两个计算你必须做对:
您可以在 numberOfSections 中计算需要多少个部分。你的计算是正确的。
您可以计算在 numberOfItemsInSection 的给定部分中显示多少单元格。您在此处的计算是不正确,因为您没有进行计算;你总是返回 3。因此每个部分总是包含 3 个单元格。
要解决此问题,您需要进行计算部分部分的计算。以下是解决此问题的一种方法:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return ((section + 1) * 3) <= self.items.count ? 3 : self.items.count % 3
}
顺便说一句,如果您有 16 个项目,如您的示例所示,最后一部分应包含 1 单元格。
关于ios - Swift 3 中的 UICollectionView 在部分中显示额外的单元格,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/39668339/
|