0

1 つのビュー コントローラーに複数の uicollectionview を配置したいのですが、それをストーリーボードに正常に配置しただけでなく、以下のような .m ファイルにコードがあります。

 -(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:    (NSIndexPath *)indexPath;
 {  

if (_collectionview.tag==2) {


Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];


 [cell.image setImageWithURL:[NSURL URLWithString:@"http://192.168.0.104:808/Images/Image1.jpg"]placeholderImage:[UIImage imageNamed:@"0.jpg"]];
    cell.label.text = [[recipes valueForKey:@"name"]objectAtIndex:0];


return cell;

}

if(_collectionview.tag==1)
{
    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:jerryCellID forIndexPath:indexPath];


    [cell.image1 setImageWithURL:[NSURL URLWithString:@"http://img1.wikia.nocookie.net/__cb20121123214954/tomandjerry/images/a/a8/Jerry_(3).jpg"]placeholderImage:[UIImage imageNamed:@"0.jpg"]];
    cell.catlabel.text = [[recipes valueForKey:@"name"]objectAtIndex:0];
    return cell;
}
else
{
Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
    [cell.image setImageWithURL:[NSURL URLWithString:@"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcT4RNUL18GXdQt2h9Ay4e7eHwm1lgC1BiRKrZY72Apt7b9cTViR7Q"]placeholderImage:[UIImage imageNamed:@"0.jpg"]];
    cell.label.text = [[recipes valueForKey:@"name"]objectAtIndex:0];
return cell;
} }

上記のコードは実行されていますが、ブレークポイントを使用してこれをデバッグした 1 つのコレクション ビューに対してのみ機能するため、最初の return ステートメントに入ります。そのため、コードの別の部分は実行されていません.soこの問題を解決する方法. 前もって感謝します :)

4

4 に答える 4

1

問題は、"_collectionView"おそらくいくつかのグローバルUICollectionViewプロパティを使用していることですが、このデリゲートメソッド"cv"はUICollectionView型を返します。

cvの代わりに使用し_collectionViewます。次のコードは完璧に機能します!!

-(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:    (NSIndexPath *)indexPath;
 {  

if (cv.tag==2) {


Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];


 [cell.image setImageWithURL:[NSURL URLWithString:@"http://192.168.0.104:808/Images/Image1.jpg"]placeholderImage:[UIImage imageNamed:@"0.jpg"]];
    cell.label.text = [[recipes valueForKey:@"name"]objectAtIndex:0];


return cell;

}

if(cv.tag==1)
{
    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:jerryCellID forIndexPath:indexPath];


    [cell.image1 setImageWithURL:[NSURL URLWithString:@"http://img1.wikia.nocookie.net/__cb20121123214954/tomandjerry/images/a/a8/Jerry_(3).jpg"]placeholderImage:[UIImage imageNamed:@"0.jpg"]];
    cell.catlabel.text = [[recipes valueForKey:@"name"]objectAtIndex:0];
    return cell;
}
else
{
Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
    [cell.image setImageWithURL:[NSURL URLWithString:@"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcT4RNUL18GXdQt2h9Ay4e7eHwm1lgC1BiRKrZY72Apt7b9cTViR7Q"]placeholderImage:[UIImage imageNamed:@"0.jpg"]];
    cell.label.text = [[recipes valueForKey:@"name"]objectAtIndex:0];
return cell;
} }
于 2015-06-16T14:02:24.710 に答える
1
  1. 両方のコレクション ビューで適切なデリゲートとデータソースが設定されていることを確認してください。

  2. NSLog を使用して、どこに問題があるか、またはどの部分が欠落しているかを調べます。

     -(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:        (NSIndexPath *)indexPath;
    {  
        NSLog(@"%d collection view is asking cell",cv.tag);
        if (_collectionview.tag==2) {
            Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
            [cell.image setImageWithURL:[NSURL URLWithString:@"http://192.168.0.104:808/Images/Image1.jpg"]placeholderImage:[UIImage imageNamed:@"0.jpg"]];
            cell.label.text = [[recipes valueForKey:@"name"]objectAtIndex:0];
            return cell;
        }
    
        if(_collectionview.tag==1)
        {
            Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:jerryCellID forIndexPath:indexPath];
            [cell.image1 setImageWithURL:[NSURL URLWithString:@"http://img1.wikia.nocookie.net/__cb20121123214954/tomandjerry/images/a/a8/Jerry_(3).jpg"]placeholderImage:[UIImage imageNamed:@"0.jpg"]];
            cell.catlabel.text = [[recipes valueForKey:@"name"]objectAtIndex:0];
            return cell;
        }else{
            Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
            [cell.image setImageWithURL:[NSURL URLWithString:@"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcT4RNUL18GXdQt2h9Ay4e7eHwm1lgC1BiRKrZY72Apt7b9cTViR7Q"]placeholderImage:[UIImage imageNamed:@"0.jpg"]];
            cell.label.text = [[recipes valueForKey:@"name"]objectAtIndex:0];
            return cell;
        } 
    }
    
于 2014-07-09T06:18:56.230 に答える
0

デリゲートまたはデータソースで取得した collectionView インスタンスを常に確認する必要があります。したがって、あなたの場合、プロパティを使用する代わりにcv.tag. _collectionViewはプロパティ (おそらく IBOutlet) であり、常に (できれば) コレクション ビューの 1 つを参照します。

コードをよりクリーンにするために、コレクション ビューのデリゲート/データソースとして機能する別のクラスを用意することをお勧めします。したがって、View Controller で、コレクション ビューの委譲を の代わりに別のクラスに設定しますself。維持しやすいでしょう。

于 2014-07-09T06:10:41.393 に答える