匹配数据时,CollectionViewCell无法正常工作

时间:2018-12-11 15:36:15

标签: ios swift uicollectionview

我在使用uicollectionview时遇到问题。 当我创建一个collectionviewcell并在collectionviewcell中创建uilabel时,突然uilabel消失了。

首先,我的故事板如下: storyBoardImage

而且,我的来源是这样的:

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!)
    }
}

启动我的应用程序时,该应用程序第一次正常运行。

enter image description here

但是当我进入文件夹并再次返回时,文件夹标签消失了。

enter image description here

当我调试时,数据是这样的:

print(“ ASJ DATA:(nameList [indexPath.row])+(indexPath.row)+(cell.folderLabel.text)+(cell.folderLabel.frame.size.height)+(cell.folderImage.frame .size.height)“)

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 谢谢。

1 个答案:

答案 0 :(得分:0)

在为folderLabel设置框架原点的任何地方都看不到(我只能看到正在设置的尺寸)。它们的关键是设置大小时,仅当文本为空时才将其设置为零。在重复使用单元格时,您需要一直设置大小,因此不再为零。

<?php
    $data = array
    (
        0 => array
        (
            0 => 'Product1',
            1 => 'Description product 1',
            2 => '10'
        ),
        1 => array
        (
            0 => 'Product2',
            1 => 'Description product 2',
            2 => '20'
        ),
        2 => array
        (
            0 => 'Product3',
            1 => 'Description product 3',
            2 => '30'
        ),
        3 => array
        (
            0 => 'Product4',
            1 => 'Description product 4',
            2 => '40'
        ),
        4 => array
        (
            0 => 'Product5',
            1 => 'Description product 5',
            2 => '50'
        )
    );

    $data = array_filter($data, function($el)
    {
        return ($el[2] >= 30 && $el[2] <= 40);
    });

    echo '<pre>'. print_r($data, 1) .'</pre>';

第二,此块的最后一行看起来不正确:

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 = ...
}