2013-11-22 25 views

回答

1

您需要同時設置集合視圖的「標籤」值,然後檢查標籤值使用以下代碼:

if (collectionView.tag == 0) { 
    // collection view 1 
} 
else if (collectionView.tag == 1) { 
    // collection view 2 
} 

您也可以在界面構建器或代碼中設置標記值。採用setTag:方法。

1

這只是一個想法

Intialize這樣的:

CGRect mainFrame = self.view.frame; 


UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; 

    collectionView1=[[UICollectionView alloc] initWithFrame:CGRectMake(0, mainFrame.origin.y , 320, 250) collectionViewLayout:layout]; 

    [collectionView1 setDataSource:self]; 
    [collectionView1 setDelegate:self]; 

    [collectionView1 registerClass:[Cell class] forCellWithReuseIdentifier:@"cellIdentifier"]; 

    [collectionView1 setBackgroundColor:[UIColor redColor]]; 

    [self.view addSubview:collectionView1]; 

    [collectionView1 reloadData]; 



    UICollectionViewFlowLayout *layout2=[[UICollectionViewFlowLayout alloc] init]; 

    collectionview2 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, mainFrame.origin.y + 270, 320, mainFrame.size.height-280) collectionViewLayout:layout2]; 

    [collectionview2 setDataSource:self]; 
    [collectionview2 setDelegate:self]; 

    [collectionview2 registerClass:[Cell2 class] forCellWithReuseIdentifier:@"destCellIdetifier"]; 

    [collectionview2 setBackgroundColor:[UIColor lightGrayColor]]; 

    [self.view collectionview2]; 

    [collectionview2 reloadData]; 

寫入數據源,並委託像下面

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    if (collectionView == collectionView1) { 
     return 18; 
    } 
    else 
     return 8; 
} 


// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Cell *cell1 = nil; 

    if ([collectionView isEqual:collectionView1]) { 
     cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 
     cell1.label.text = [NSString stringWithFormat:@"%d",indexPath.item]; 
    } 
    else { 

     Cell2 *cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"destCellIdetifier" forIndexPath:indexPath]; 
     cell2.label.text = [NSString stringWithFormat:@"%d",indexPath.item]; 
     return cell2; 

    } 

    return cell1; 
} 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return CGSizeMake(65, 60); 
} 

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { 

    return UIEdgeInsetsMake(10.0f, 10, 10.0f, 10.0f); 
} 
+0

什麼是大型機哪個變量 – user241641

+0

檢查更新的答案。 –

相關問題