2013-11-22 4 views
1

Я хочу сохранить несколько элементов управления uicollectionview в одном ViewController .PL предложить мне код или любые ссылкиКак сохранить несколько элементов управления uicollectionview в одном ViewController

+0

Дайте различные теги для обоих элементов управления сбора, и делегатом методы проверки для конкретных контроль коллекции по тегу. –

+0

http://skeuo.com/uicollectionview-custom-layout-tutorial использовать это – Sport

+0

Я хочу добавить несколько uicollectionview как Subview – user241641

ответ

1

Вы должны установить «тег» значение как представления коллекции, а затем проверить значение тега, используя следующий код:

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

Вы можете установить значение тега в интерфейсе строителя или в коде, а также. используя setTag: способ.

1

Это просто идея

инициализировать так:

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]; 

Написать DataSource и делегат, как показано ниже

- (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

проверить обновленный ответ. –

Смежные вопросы