我使用自定義UICollectionView
佈局具有不同大小的單元格,我不能在這個部分。多個UICollectionView在同一個ViewController
在同一UIViewController
中使用多個UICollectionView
s是不錯的主意嗎?有什麼建議嗎?
我使用自定義UICollectionView
佈局具有不同大小的單元格,我不能在這個部分。多個UICollectionView在同一個ViewController
在同一UIViewController
中使用多個UICollectionView
s是不錯的主意嗎?有什麼建議嗎?
很簡單
爲每UICollectionView代表
- (NSUInteger)maximumNumberOfColumnsForCollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
- (CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
heightForItemAtIndexPath:(NSIndexPath*)indexPath
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView
cellForItemAtIndexPath:(NSIndexPath*)indexPath
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section`
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView`
寫內部條件:
if (collectionView == myCollectionView1)
{
// do this
}
else if (collectionView == myCollectionView2)
{
// do this
}
讓在
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
if (collectionView == myCollectionView1)
{
return 12;
}
else if (collectionView == myCollectionView2)
{
return 7;
}
return 0;
}
使用Tag屬性FO比方說r在一個View Controller中區分多個UICollectionView或UITableView。現在
firstCollectionView.tag = 1;
secondCollectionView.tag = 2;
and so on ...
,在委託方法使用,如果檢查UICollectionView
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if(collectionView.tag == 1)
{
//cell for first UICollectionView or CollectionView with tag equal to 1
}
else if(collection.tag == 2)
{
//cell for second UICollectionView or CollectionView with tag equal to 2
}
}
使用相同的技術在其他委託方法。
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (collectionView ==_tbl_Catogary) {
return 10;
}
if (collectionView == _tbl_SubCatogary) {
return 4;
}
if (collectionView == _tbl_NearbyAds) {
return 8;
}
else
return 21;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *mycell;
if(collectionView == _tbl_Catogary){
CatogaryCVC *cell = (CatogaryCVC *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CatogaryCVC" forIndexPath:indexPath];
mycell =cell;
}
if (collectionView == _tbl_SubCatogary) {
SubcatogaryCVC *cell = (SubcatogaryCVC *)[collectionView dequeueReusableCellWithReuseIdentifier:@"SubcatogaryCVC" forIndexPath:indexPath];
mycell=cell;
}
if (collectionView == _tbl_NearbyAds) {
NearbyAdCVC *cell = (NearbyAdCVC *)[collectionView dequeueReusableCellWithReuseIdentifier:@"NearbyAdCVC" forIndexPath:indexPath];
mycell = cell;
}
if (collectionView == _tbl_popularAds) {
PopularAdCVC *cell = (PopularAdCVC *) [collectionView dequeueReusableCellWithReuseIdentifier:@"PopularAdCVC" forIndexPath:indexPath];
mycell = cell;
}
return mycell;
}
重命名爲您的收藏視圖出口
請更詳細地解釋一下上面的代碼是如何解答OP的問題。 – Nae