我想保持多個uicollectionview控制在相同的ViewController .PL建議我的代碼或任何鏈接如何保持多uicollectionview控制在相同的ViewController
1
A
回答
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
檢查更新的答案。 –
相關問題
- 1. 多個UICollectionView在同一個ViewController
- 2. 如何在相同的viewController
- 3. 保持一個ViewController在相同的狀態/保持它在後臺加載?
- 4. UICollectionView內的ViewController
- 5. 翻轉ViewController但保持與Storyboard相同的NavigationController
- 6. 如何在不使用$ watch的情況下在多個控制器上保持相同的服務數據?
- 7. 如何加入多對多並保持相同總量
- 8. 如何保持多個Java HttpConnections開到相同的目的地
- 9. 如何保持動畫上UICollectionView
- 10. 如何過濾UICollectionView並保持鍵盤?
- 11. 多個barplots保持相同的軸和相同的條寬
- 12. 如何處理導航控制器和ViewController的多點觸控
- 13. iOS:如何使用相同的Popover ViewController來控制兩個單獨的按鈕
- 14. 如何加入一對多並保持相同的總金額
- 15. 如何在多個控制器中使用相同的數據?
- 16. 如何顯示/隱藏html控件保持其位置相同?
- 17. 如何而X軸保持相同
- 18. UINavigationController - 相同的viewController
- 19. 具有相同背景的多個ViewController
- 20. 相同的viewController彈出多次
- 21. 如何保持用戶控制值?
- 22. 如何保持打開控制檯?
- 23. 如何保持動態控制屬性
- 24. 如何保持控制特有的CSS與控制代碼(ASP.NET)
- 25. 使用的viewController打開相同的viewController
- 26. Angular:如何保持$ scope屬性與控制器函數同步?
- 27. Swift3,如何在不同的viewController中使用相同的UITableView
- 28. 如何保持一個變量方便跨多個控制器
- 29. MySQL複製如何保持同步?
- 30. 保持在不同的回購相同的文件在不同的版本控制系統
給出不同的標籤,這兩個集控,並在委託方法檢查特定通過標籤收集控制。 –
http://skeuo.com/uicollectionview-custom-layout-tutorial使用此 – Sport
我想添加多個uicollectionview像子視圖 – user241641