2017-08-09 33 views
0

我想獲得UICollectionView中的所有項目。是否有可能在iOS中獲取UICollectionView中的所有項目?

我對下面的代碼進行了編碼,但它並不按我想要的方式工作。我的CollectionView中有6個項目,但只顯示其中4個項目。我應該如何修改我的母鹿?

for(NSIndexPath *indexPath in self.viewCollection.indexPathsForVisibleItems) { 
    // for(imgCount in self.viewCollection.numberOfSections) { 
    //  NSIndexPath *indexPath = self.viewCollection.indexPathsForVisibleItems; 
      // Get selected cell 
      MYCell* cell = (MYCell*) [_viewCollection cellForItemAtIndexPath:indexPath]; 
      if(cell.checked == YES) { 
       imgCount = imgCount + 1; 

       NSString *imgName = [directoryContent objectAtIndex:indexPath.item]; 
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
       NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", imgName]]; 
       NSData *imgFile = [NSData dataWithContentsOfFile:filePath]; 

       if(imgCount == 1) { 
        [IMGDATA_grid appendString:[imgFile base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength]]; 
       } 
       else { 
        [IMGDATA_grid appendString:@","]; 
        mTempData = [imgFile base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength]; 
        [IMGDATA_grid appendString:mTempData]; 
       } 
      } 
    } 
+4

您已擁有收藏視圖數據源中的所有數據。無需遍歷項目。 – rmaddy

+0

您的收藏視圖可以製作足夠的單元格以填充整個屏幕。當用戶滾動時,集合視圖將消失在屏幕一端的項目並在屏幕的另一端重新使用它們,這就是爲什麼使用'collectionView.dequeueReusableCell(...)'創建單元格的原因。 – Palle

+0

您的單元格只是您模型中數據的視圖。從模型中獲取數據,而不是從單元格中獲取數據。 – Paulw11

回答

0

的方式CollectionViews(和TableViews等)工作,是你通過他們的數據從一種結構,你的代碼中,最有可能的一個NSArray或一個NSMutableArray。這是你保存數據的地方。在陣列

穿戴數據=>讓你的CollectionView訪問數據=>顯示UI

如果需要其他地方訪問數據在代碼,直接引用的數據結構,而不是UI元素本身特別是因爲系統可以移除單元以節省內存。

所以說,你想知道什麼標題是UICollectionViewCell在47排,你不需要去細胞47,並要求其標題,而是直接引用您的數據結構,如:

NSString * title = myDataArray[47]; 
0

你好,我認爲這是關於你的集合視圖算法。我的意思是使用 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath方法,你應該保留你在單元格,數組或模型或類似的東西上使用的數據。可能顯示的4個項目,是在viewDidLoad沒有任何滾動操作後繪製的項目。如果在collectionview中使用相同偏移量的3個單元格,則可以毫無問題地獲取所有項目。關於我的意思

NSArray *array = [NSArray arrayWithObjects:@"String1",@"String2",@"String3","String4",@"String5",@"String6",nil]; 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
CustomCollectionViewCell *cell = (CustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCollectionViewCell" forIndexPath:indexPath]; 

if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCollectionViewCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 

[cell setupCellWithString:[array objectAtIndex:indexPath.row]]; 

return cell;} 

在集合視圖中選擇任何項目微小的代碼

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 

NSString *strInArray = [array objectAtIndex:indexPath.row];} 

我希望我能正確理解你的問題。祝你好運。順便說一下,collectionview.indexPathsForVisibleItems表示在collectionview上繪製和顯示的項目。

相關問題