2017-07-28 44 views
1

我是新來的Objective-C的,我有接受的NSArray作爲參數的函數,現在我想在一個局部變量數組存儲在我的功能,然後通過填充集合視圖那個數組,ps我還必須更新cellForItemAtIndexPath委託方法在同一個類中。使用NSArray的填充的CollectionView

這是tableView的Cell類中的方法。在這裏我已經在cell中嵌入了一個collectionView。

-(void)initCellWithSetsArray:(NSArray *)setsArray{ 

NSMutableArray *setArray = [NSMutableArray array];  
} 

這是我在這裏調用接受cellForRowAt tableView的委託方法中的數組的方法。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     [cell initCellWithSetsArray :self.setsHistoryArray]; 
} 

所以我需要立足cellForRowAtIndexPath叫當FUNC解析陣列上更新的CollectionView。我希望你們現在能夠得到這張照片。

+0

你有你可以顯示任何代碼?收集視圖中有許多教程可用。您不能將數組存儲在局部變量中;你需要將它存儲在你的視圖控制器的屬性中,因爲它需要'cellForItemAt'可用。 – Paulw11

+0

發佈你的代碼,所以我們可以得到更好的想法並嘗試解決你的問題。 – Nirmalsinh

+0

@ Paulw11實際上這個函數在TableViewCells的類中,並且一個集合視圖嵌入在它的單元格的nib文件中。所以我在tableView的委託方法的cellforRowAtIndexPath中調用這個函數,並且我還需要更新collectionview與我在解析單元格類中的函數時使用的相同數組。 –

回答

1

您需要將屬性添加到您的類來保存數組:

@property (strong, nonatomic) NSArray *setsArray; 

-(void) setSetsArray(NSArray *)sets { 
    _setsArray = sets; 
    [self.collectionView reloadData]; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView 
numberOfItemsInSection:(NSInteger)section { 
    return [self.setsArray count]; 
} 

你可以在你的課堂上使用這個陣列中的cellForItemAt方法。

而且,在你的類prepareForReuse你應該清除現有的陣列並重新加載集合視圖:

-(void)prepareForReuse { 
    self.setsArray = nil; 
    [self.collectionView reloadData]; 
} 

然後,你可以簡單地分配財產的cellForRowAt:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    MyCellClass *cell = (MyCellClass *)[tableView dequeueReusableCellWithIdentifier:"MyCellIdentifier" forIndexPath:indexPath]; 
    cell.setsArray = self.setsHistoryArray; // Note: This looks wrong; - you should normally use indexPath.row to get the right data for this row 
    return cell; 
} 
0

試試這個;

編譯標記的CollectionView

-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 
    return 1; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    return arrNames.count; 
} 

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


    UILabel *lblName = (UILabel *)[cell viewWithTag:101]; // set tag in storyboard for this label 
    lblName.text = arrNames[indexPath.item]; 

    return cell; 
} 
+0

請參閱編輯的問題並作出相應的迴應。 –

+0

您的收藏視圖中是否有多個部分? – KKRocks

+0

有兩個視圖,例如單元格中的視圖A和視圖B,並且我在視圖A中嵌入了collectionView –