UICollectionView一些单元既不是可见项也不是出列单元

Chr*_* R. 3 ios uicollectionview swift3

我在我的UICollectionView中有一个问题,其中一些单元格似乎既不是它的一部分indexPathForVisibleItems,也不是从缓存队列中取出的dequeueReusableCell.结果是,某些单元格在滚动期间未收到所需的数据更新并显示旧行为.

为简单起见,我将项目简化为必要的控制器和最小化的故事板.基本上,我有一个NavigationController作为EntryPoint,它包含MainViewController,它本身包含一个带有CollectionViewController的ContainerView.

NavigationController使用默认编辑按钮在编辑和非编辑模式之间切换 - 这将导致在编辑模式下在单元格上显示图像.因此,我实现setEditing并更改了所有可见单元格的图像隐藏属性,另外我在出列时设置了图像隐藏属性 - 假设单元格可见或将来会出列.

这个工作正常,而CollectionView从上到下滚动.但是当我从编辑模式切换回非编辑模式同时滚动到底部然后滚动回到顶部时,一些单元仍然显示图像(更具体:至少相同的行,这是第一个非可见行).不知怎的,我假设出列的单元格和可见单元格将是显示数据的互补部分,这应该导致图像在setEditing调用期间隐藏/取消隐藏(适用于前4行单元格)或隐藏/ dehuing期间取消隐藏(适用于最后几行,除了我的示例中的第三行)

CollectionViewController的代码:

import Foundation
import UIKit
import Photos
class CollectionViewController : UICollectionViewController {

    fileprivate let CELL_ID = "PicCell"
    fileprivate let IMAGE_VIEW_SIZE = 104
    var selectedIndex = -1
    var itemCount = 28

    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        print(self.isEditing)
        collectionView?.allowsMultipleSelection = editing
        for indexPath in (collectionView?.indexPathsForSelectedItems)! {
            collectionView?.deselectItem(at: indexPath, animated: true)
        }

        for indexPath in (collectionView?.indexPathsForVisibleItems)! {
            let cell = collectionView?.cellForItem(at: indexPath) as? PicCell
            if cell != nil {
                cell?.editing = editing
            }
        }
    }

    override func viewDidLoad() {
        self.navigationItem.rightBarButtonItem = self.editButtonItem
        collectionView?.reloadData()
    }
}

extension CollectionViewController {
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount;
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        print(indexPath.row)
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CELL_ID, for: indexPath) as! PicCell
        cell.editing = isEditing
        cell.isSelected = false
        return cell;
    }
}
Run Code Online (Sandbox Code Playgroud)

PicCell的代码

import Foundation
import UIKit
class PicCell : UICollectionViewCell {

    @IBOutlet weak var deleteBtn: UIImageView!
    var editing:Bool = false{
        didSet {
            self.deleteBtn.isHidden = !editing
            self.deleteBtn.tintColor = UIColor.blue
        }
    }

    override var isSelected: Bool{
        didSet {
            if isSelected  && editing {
                self.deleteBtn.tintColor = UIColor.red
            } else {
                self.deleteBtn.tintColor = UIColor.blue
            }
        }
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        self.editing = false
        self.isSelected = false
    }
}
Run Code Online (Sandbox Code Playgroud)

MainViewController的代码

import Foundation
import UIKit
class MainViewController: UIViewController {

    override func viewDidLoad() {
        self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        collectionController?.setEditing(editing, animated: animated)
    }

    var collectionController : CollectionViewController? = nil
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "ShowCollectionView" {
            collectionController = segue.destination as! CollectionViewController
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我在一个部分中使用常量值来计算项目数,这应该不是什么大问题.

我不知道它是否重要,但是我的单元格宽度和高度都是104像素,图像呈现为模板,UIContainerView宽度为343,高度为473,我正在使用iOS 10.1在iPhone 7+模拟器上进行测试

如果故事板上有任何遗漏,我可能会添加一些截图,但我不确定要准确发布什么

在此先感谢您的帮助和亲切的问候

基督教

编辑:只是为了清楚我的问题:我正在为我的代码中的明显错误或访问UICollectionViewCells寻找一些建议,这些UICollectionViewCell既不在可见项目列表中也不在滚动期间出列 - 可能有办法访问那些我不知道的细胞

Rob*_*Rob 7

如果关闭预取,则可以更改此行为:

collectionView?.isPrefetchingEnabled = false
Run Code Online (Sandbox Code Playgroud)

或者你可以保持它,但然后:

  • 钩入UICollectionViewDelegate方法didEndDisplayingwillDisplay知道细胞出现和消失独立cellForItemAt; 要么

  • 让单元格在某些视图控制器属性上执行某些KVO,或观察视图控制器将启动的一些通知,以便知道是否更改其状态.