我有一个集合视图,其中的单元格会根据用户选择的大小而改变大小:小,中,大。我有协议UICollectionViewDelegateFlowLayout和委托函数:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
该函数可以正确设置单元格所需区域的大小,但不会调整单元格本身的大小。
这是将单元格设置为较小设置后的视图:
以下是具有默认大小的视图:
如您所见,单元格的空间已更改,但单元格大小未更改。
在情节提要中,无论单元格大小设置为多少,无论我在代码中做什么,大小都会是多少。有谁知道我该如何覆盖:
任何帮助将不胜感激。谢谢
正在调用委托函数。大小在其他页面上更改。该大小作为一个值存储在一个单例中,委托函数根据该值决定要使用的大小。这是委托函数:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { // if self.collectionView == collectionView { let viewHeight = collectionView.frame.height let smallSize = (viewHeight / 4) - 20 let mediumSize = (viewHeight / 2) - 20 let largeSize = viewHeight - 20 switch gridSquareSize { case .small: return CGSize(width: smallSize, height: smallSize) case .standard: return CGSize(width: mediumSize, height: mediumSize) case .large: return CGSize(width: largeSize, height: largeSize) } //} }
这是设置单元格的功能:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "galleryCell", for: indexPath)
as! GalleryCell
cell.layoutIfNeeded()
switch SettingsModel.instance.currentGridSquareSize {
case .small:
cell.galleryName.font = cell.galleryName.font.withSize(12)
break
case .standard:
cell.galleryName.font = cell.galleryName.font.withSize(24)
break
case .large:
cell.galleryName.font = cell.galleryName.font.withSize(36)
break
}
cell.isHidden = false
cell.background.backgroundColor = UIColor.white
cell.plusIcon.isHidden = true
cell.topLeftImage.isHidden = false
cell.galleryName.textColor = UIColor(red: 45, green: 64, blue: 83, alpha: 1)
cell.topLeftImage.image = UIImage(named: "PlaceholderImage")
cell.delete.isHidden = !isInEditMode
let position = indexPath.row
if position == mGalleriesList.count {
if isInEditMode {
cell.background.backgroundColor = UIColor.darkGray
cell.galleryName.text = "Add new gallery"
cell.galleryName.textColor = UIColor.white
cell.plusIcon.isHidden = false
cell.topLeftImage.isHidden = true
cell.delete.isHidden = true
}
else {
cell.isHidden = true
}
}
else {
let gallery = mGalleriesList[position]
cell.data = gallery
cell.plusIcon.isHidden = true
if let galleryName = gallery.galleryName {
cell.galleryName.text = galleryName
}
var mediaEntries = [MediaEntry]()
for entry in gallery.mediaEntries {
if let deleted = entry.deleted {
if !deleted {
mediaEntries.append(entry)
}
}
else {
mediaEntries.append(entry)
}
}
if mediaEntries.count > 0 {
if let downloadUrl = mediaEntries[0].downloadUrl {
setImage(storageUrl: downloadUrl, imageView: cell.topLeftImage, placeholder: placeholder!, cropToBounds: false)
}
}
}
return cell
}
这也是情节提要中单元格的屏幕截图:
希望这些附加信息有帮助