如何在出现之前访问将要出现的单元格?

How to access cells that will appear, before they appear?

我有一个集合视图,但我无法访问未出现在屏幕上的单元格,1...5 好,以及索引部分第 6 行、第 7 行的单元格,必须滚动才能看到它们

func Action0(sender: UIButton!) {
    for i in 0...7 {
        if i != 0{
        var indexPath = NSIndexPath(forRow: i, inSection: 0)
        var cell = collectionView!.cellForItemAtIndexPath(indexPath)
        cell!.backgroundColor = UIColor.whiteColor()
        }else{
        var indexPath = NSIndexPath(forRow: 0, inSection: 0)
        var cell = collectionView!.cellForItemAtIndexPath(indexPath)
        cell!.backgroundColor = UIColor.blueColor()
        }
    }
}

如何访问那些还不可见的单元格?

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
    cell.backgroundColor = UIColor.blackColor()
    cell.textLabel.text = "\(indexPath.section):\(indexPath.row)"
    cell.buttonView.setBackgroundImage(UIImage(named: "Good"), forState: UIControlState.Normal)
    cell.buttonView.addTarget(self, action: Selector("Action\(indexPath.row):"), forControlEvents: UIControlEvents.TouchUpInside)
    return cell
}

尚不可见的单元格不存在。 Table 视图和集合视图仅创建当前可见的单元格。当您滚动时,离开视图的单元格将被回收并重新用于进入视图的新索引路径。

您需要考虑数据模型和单元格,数据模型保存每个索引路径的数据,单元格显示部分数据的子集视图。