我在使用uicollectionview时遇到问题。
当我创建一个collectionviewcell并在collectionviewcell中创建uilabel时,突然uilabel消失了。
首先,我的故事板就像:
而且,我的来源是这样的:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = folderCollectionView.dequeueReusableCell(withReuseIdentifier: "FolderCollectionViewCell", for: indexPath) as! FolderCollectionViewCell
cell.folderLabel.frame.size.width = cellSize
cell.folderLabel.frame.size.height = 20
cell.folderImage.frame.size.width = cellSize
cell.folderImage.frame.size.width = cellSize
cell.folderLabel.text = ""
cell.folderLabel.textColor = UIColor.flatWhite
if(nameList[indexPath.row] == "")
{
cell.folderLabel.text = ""
cell.folderLabel.frame.size = CGSize(width: 0, height: 0)
cell.folderImage.frame.size = CGSize(width: cellSize, height: cellSize + 20)
}
cell.folderImage.image = imgList[indexPath.row]
cell.folderLabel!.text = nameList[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
if(nameList[indexPath.row] != "")
{
if(nameList[indexPath.row] != "⤴︎")
{
currentAlbum = nameList[indexPath.row]
}
var selectedAlbum = ""
selectedAlbum = nameList[indexPath.row]
setImageInformation(albumName: selectedAlbum)
folderCollectionView.reloadData()
collectionView.performBatchUpdates({UIView.animate(views: self.folderCollectionView.orderedVisibleCells, animations: animations, completion: nil)} , completion: nil)
}
else
{
let imageData = imgList[indexPath.row].jpegData(compressionQuality: 1.0)
goToImageViewer(data : imageData!)
}
}
当我启动我的应用程序时,该应用程序第一次可以正常运行。
但是当我进入文件夹并再次返回时,文件夹标签消失了。
当我调试时,数据是这样的:
print(“ ASJ DATA:(nameList [indexPath.row])+(indexPath.row)+(cell.folderLabel.text)+(cell.folderLabel.frame.size.height)+(cell.folderImage.frame.size。高度)”)
ASJ数据:dfgdfgd + 0 +可选(“”)+ 20.0 + 105.0
cell.folderLabel.text:可选(“ dfgdfgd”)
ASJ数据:dgd + 1 +可选(“”)+ 20.0 + 105.0
cell.folderLabel.text:可选(“ dgd”)
ASJ数据:dsdfsdf + 2 +可选(“”)+ 20.0 + 105.0
cell.folderLabel.text:可选(“ dsdfsdf”)
ASJ数据:sdf + 3 +可选(“”)+ 20.0 + 105.0
cell.folderLabel.text:可选(“ sdf”)
ASJ数据:sdff + 4 +可选(“”)+ 20.0 + 105.0
cell.folderLabel.text:可选(“ sdff”)
我的消息来源对此有任何想法吗?
这让我发疯,所以我需要一些帮助... T_T
谢谢。
最佳答案
我没有在任何地方设置FolderLabel的帧原点(我只能看到正在设置的大小)。它们的关键是设置大小时,仅当文本为空时才将其设置为零。随着单元的重用,您需要一直设置大小,因此不再为零。
if(nameList[indexPath.row] == "")
{
cell.folderLabel.text = ""
cell.folderLabel.frame.size = CGSize(width: 0, height: 0)
cell.folderImage.frame.size = CGSize(width: cellSize, height: cellSize + 20)
}
else // Add this
{
cell.folderLabel.frame.size = ...
}
其次,该块的最后一行看起来是错误的:
cell.folderLabel.frame.size.width = cellSize
cell.folderLabel.frame.size.height = 20
cell.folderImage.frame.size.width = cellSize
cell.folderImage.frame.size.width = cellSize // should be size.height
关于ios - 匹配数据时,CollectionViewCell无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53727485/