0

我想在一个视图控制器中放置多个 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;
} }

上面的代码正在运行,但它仅适用于一个集合视图,我使用断点对其进行了调试,因此它进入第一个返回语句。所以另一部分代码甚至没有运行。那么如何解决这个问题。先感谢您 :)

4

4 回答 4

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 回答
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 回答
0

尝试以下代码:

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

    if (collectionView.tag==2) 
    {
        cell = [collectionView 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];
    }
    if(collectionView.tag==1)
    {
        cell = [collectionView 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];
    }
    else
    {
        cell = [collectionView 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-09T10:04:28.953 回答
0

您需要始终检查在委托或数据源中获得的 collectionView 实例。因此,在您的情况下,不要使用属性使用cv.tag. _collectionView是一个属性(也许是 IBOutlet),它总是指您的一个(希望)您的集合视图。

为了使您的代码更干净,我建议使用单独的类作为您的集合视图的委托/数据源。因此,在您的视图控制器中,将集合视图委托设置为该单独的类,而不是self. 维护起来会更容易。

于 2014-07-09T06:10:41.393 回答