【问题标题】:How to access cells that will appear, before they appear?如何在它们出现之前访问将出现的单元格?
【发布时间】:2015-05-22 23:38:15
【问题描述】:

我有一个集合视图,但没有出现在屏幕上的单元格我无法访问,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
}

【问题讨论】:

    标签: ios swift collectionview


    【解决方案1】:

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

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

    【讨论】:

    • 我添加到帖子中的这个功能是创建和/或回收的功能吗?
    • @JosipBogdan 在您的代码中,collectionView.dequeueReusableCellWithReuseIdentifier(_:forIndexPath:) 创建或回收现有视图。
    • 我尝试了一些东西,但仍然无法正常工作,我可以再推一点吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多