CollectionView单元格混乱

时间:2018-10-27 02:12:21

标签: swift collectionview

当您点击CollectionView Cell时,我正在忙于带有音板的语音应用程序,然后iPhone会说出文字。我的应用程序中有一个小错误,我不知道是什么原因。

我已经建立了一个CollectionView并将图像作为backgroundViews。它可以正常工作,但是当我转到另一个视图(例如“绘画”视图)时,我将返回,则单元格将造成混淆。

有人可以告诉我哪里出了问题以及如何解决吗?

谢谢!

enter image description here

这是我的代码:

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    loadData()

    let itemSize = UIScreen.main.bounds.width/2 - 5
    let itemHeight = itemSize / 2
    let layout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets.init(top: 3, left: 3, bottom: 3, right: 3)
    layout.itemSize = CGSize(width: itemSize, height: itemHeight)

    layout.minimumInteritemSpacing = 3
    layout.minimumLineSpacing = 3

    soundboard.collectionViewLayout = layout

    navigationItem.rightBarButtonItem = editButtonItem

    if(traitCollection.forceTouchCapability == .available){
        registerForPreviewing(with: self as UIViewControllerPreviewingDelegate, sourceView: collectionView)
    }
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    loadData()
}

func loadData() {
    let soundRequest:NSFetchRequest<Soundboard> = Soundboard.fetchRequest()

    do {
        soundBoardData = try managedObjectContext.fetch(soundRequest)
        self.soundboard.reloadData()
    } catch {
        print("Error")
    }
}


// MARK: - Collection View
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return soundBoardData.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> soundboardCellVC {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! soundboardCellVC

    let soundItem = soundBoardData[indexPath.row]


    if let getCellPhoto = soundItem.photo as Data? {

        cell.title.text = "";

        let cellPhoto = UIImage(data: getCellPhoto)


        let cellPhotoFrame = UIImageView(image:cellPhoto)


        cellPhotoFrame.frame = CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height)
        cellPhotoFrame.contentMode = UIView.ContentMode.scaleAspectFill

        cell.backgroundView = UIView()
        cell.backgroundView!.addSubview(cellPhotoFrame)
    }
    else
    {
        cell.title.text = soundItem.title;

        cell.backgroundColor = UIColor(red: CGFloat(soundItem.colorRed), green: CGFloat(soundItem.colorGreen), blue: CGFloat(soundItem.colorBlue), alpha: 1)

        let fontAwesomeIcon = FAType(rawValue: Helper.FANames.index(of: soundItem.icon!)!)

        cell.icon.setFAIconWithName(icon: fontAwesomeIcon!, textColor: UIColor.white)
    }

    cell.layer.cornerRadius = 10
    cell.layer.masksToBounds = true

    cell.delegate = self as SoundboardCellDelegate

    return cell
}

2 个答案:

答案 0 :(得分:0)

单元格将被重复使用,这意味着您将向具有现有图像视图的单元格添加新的图像视图。

您应该在UIImageView类中创建一个soundboardCellVC,并简单地将图像放入其中,或根据需要将图像设置为nil

其他一些风格要点:

按照惯例,类应以大写字母开头,并且是单元格子类,而不是View Controller子类,因此应该类似于SoundboardCell而不是soundboardCellVC

最后,您的cellForItemAt签名应与协议声明匹配,并声明为返回UICollectionViewCell,而不是您的特定单元格子类:

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

答案 1 :(得分:0)

处理单元格时的一个好习惯是清除在单元格被重用时不想保留的所有属性。

https://developer.apple.com/documentation/uikit/uicollectionreusableview/1620141-prepareforreuse

例如,在您的单元格类中,您可以执行以下操作:

override func prepareForReuse() {
    super.prepareForReuse()
    self.title.text = nil
}

这可以使您的cellForItem方法更干净,并且您可以肯定在重新使用它之前 已经执行了上述操作。