循环遍历 UICollectionview 中的所有单元格 [英] looping through all cells in UICollectionview

查看:52
本文介绍了循环遍历 UICollectionview 中的所有单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UICollection,其中有学生项目的数量,每个项目内部都有用于记录出勤的开关.我像这样循环遍历所有可见单元格.

I have UICollection in which there will be number of Students items and each item having switch inside it used for recording attendance. I am loop through all visible cells like this.

for(attendancecollectionViewCell* cells in [[self collectionView] visibleCells]){

    NSLog(@"The switch value : %c",cells.attendanceSwitchLabel.isOn);

}

但是,我想遍历所有单元格以进行出勤,而不仅仅是可见单元格.

But, I wanted to loop through all the cells for attendance, not just the visible cells.

推荐答案

您不能遍历不可见的单元格,因为这些单元格不存在.UICollectionView 与 UITableView 一样,在单元格离开屏幕时立即重用它们.即,如果您向下滚动,它会将一个已被滚动的单元格用于将要滚动到视图中的新"单元格.

You cannot loop through non-visible cells because those cells don't exist. UICollectionView, like UITableView, reuses cells as soon as they are offscreen. I.e., if you scroll down, it takes a cell that has been scrolled off and uses it for a "new" cell about to be scrolled into view.

如果您希望为集合中的条目保留状态,则必须将其与单元格本身分开存储.例如,映射到 indexPath.row 值的结构体(或自定义 NSObjects)的 NSArray.

If you wish to hold state for an entry in your collection, you'll have to store it separately from the cell itself. For example, an NSArray of structs (or custom NSObjects) that map to the indexPath.row value.

对您来说更重要的问题是:您想在 for 循环中实现什么目标?

A more important question for you specifically would be: What are you trying to achieve in your for loop?

如果您需要更多信息或示例代码,请告诉我.

Let me know if you need more information or sample code.

这篇关于循环遍历 UICollectionview 中的所有单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆