2014-11-08 34 views
-1

我正在尋找檢索數據,如可見的單元格內的label.text,我不知道這將是可能的。是否有可能從collectionViewCell中檢索數據?

純上下文:我有一個collectionView顯示問題(每個單元格顯示一個問題) collectionView是在UIView內。

在這個相同的uiview中,我有一個按鈕,當按下它時,應該驗證問題答案。 在這一點上,我所做的唯一事情是將@IBAction鏈接到按鈕,以便從可見單元格打印問題(只有一個單元格可見)。

這裏是我如何構建按鈕觸摸細胞

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 
    } 

(按鈕出細胞)。

我知道visibleCells()存在,但它看起來不像我可以從中檢索信息。

 for cell in self.collectionView!.visibleCells() as [UICollectionViewCell] { 

     } 

有什麼辦法嗎?

+0

這個問題需要更多的細節。按鈕上的對象是什麼?什麼是IBAction綁定?那個班級怎麼知道細胞?在普通英語邏輯中,該類如何知道應該爲哪個單元獲取數據? – nhgrif 2014-11-08 18:21:28

+0

這基本上是我在問什麼。我已經更新了我的問題 – SKYnine 2014-11-08 18:29:06

+0

我仍然沒有看到按鈕應該知道哪個單元格需要擔心。你想要它來處理每個細胞? – nhgrif 2014-11-08 18:36:40

回答

0

一個可見的細胞內訪問數據(如label.text),你可以使用visibleCells()方法如下:

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) 
     } 
0

您已擁有自定義集合視圖單元類。顯而易見的解決方案是將PFObject傳遞給cellForItemAtIndexRow方法中的CollectionViewCell,然後在單擊按鈕時從單元中檢索它。

只需將visibleCells的返回值轉換爲您的集合視圖單元類,然後將PFObject從中拉出即可。

相關問題