问题描述
我想从可见单元格中检索数据,例如 label.text,但我不知道这怎么可能.
I'm looking to retrieve data, such as a label.text from within a visible cell, and I don't know how that would be possible.
简单的上下文:我有一个显示问题的 collectionView(每个单元格显示一个问题)collectionView 在 UIView 中.
Plain context: I have a collectionView displaying questions (each cell display a question)the collectionView is within a UIView.
在同一个 uiview 中,我有一个按钮,按下时应验证问题答案.在这一点上,我唯一要做的就是将 @IBAction 链接到按钮,以便它从可见单元格中打印问题(只有一个单元格可见).
Within this same uiview I have a button which, when pressed, should validate the question answer.At this point the only thing Im doing is linking a @IBAction to the button in order for it to println the question from the visible cell (only one cell is visible).
这是我构造单元格的方法
here is how I construct the cell
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// return arr.count
return questionData.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:CollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as CollectionViewCell
let thereq:PFObject = self.questionData.objectAtIndex(indexPath.row) as PFObject
// cell.contentView.frame = cell.bounds
var action = thereq.objectForKey("action") as String
var what = thereq.objectForKey("what") as String
cell.askingQuestionLabel.text = "where \(action) \(what)"
return cell
}
按钮触摸(按钮在单元格外).
on button touch (button is out of the cell).
我知道 visibleCells()
存在,但看起来我无法从中检索信息.
I know visibleCells()
exists but it doesn't look like I can retrieve info out of it.
for cell in self.collectionView!.visibleCells() as [UICollectionViewCell] {
}
有什么办法吗?
推荐答案
要访问可见单元格内的数据(例如label.text),可以使用visibleCells() 方法如下:
To access data (such as a label.text) within a visible cell, you can use the visibleCells() method as follow:
for item in self.collectionView!.visibleCells() as [UICollectionViewCell] {
var indexpath : NSIndexPath = self.collectionView!.indexPathForCell(item as CollectionViewCell)!
var cell : CollectionViewCell = self.collectionView!.cellForItemAtIndexPath(indexpath) as CollectionViewCell
//access cell data
println(cell.labelName.text)
}
这篇关于是否可以从 collectionViewCell 中检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!