我有一个集合视图,但是没有出现在屏幕上我无法访问的单元格,1 ... 5 ok,以及索引parth第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
}
答案 0 :(得分:3)
不可见的单元格不存在。表视图和集合视图仅创建当前可见的单元格。滚动时,超出视图的单元格将被回收并重新用于进入视图的新索引路径。
您需要考虑数据模型和单元格,数据模型包含每个索引路径的数据,单元格显示某些数据的子集视图。