0
我有一個集合視圖,我正在尋找一種方法來查明單元格的大小可以適合每行4個單元格(在特定的屏幕大小上)並使用在所有單元格上的大小。我有一個可重用的單元格,用於其他單元格。如何自動調整單元格大小以適合每行4行
我有一個集合視圖,我正在尋找一種方法來查明單元格的大小可以適合每行4個單元格(在特定的屏幕大小上)並使用在所有單元格上的大小。我有一個可重用的單元格,用於其他單元格。如何自動調整單元格大小以適合每行4行
extension LevelSelectController: UICollectionViewDelegateFlowLayout {
// Collection view flow layout setup
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// Compute the dimension of a cell for an NxN layout with space S between
// cells. Take the collection view's width, subtract (N-1)*S points for
// the spaces between the cells, and then divide by N to find the final
// dimension for the cell's width and height.
let cellsAcross: CGFloat = 4
let spaceBetweenCells: CGFloat = 0
let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells)/cellsAcross
return CGSize(width: dim, height: dim)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 0, 0.0, 0.0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}