iPad中单个视图控制器中的多个UICOLLECTION视图



我想在一个视图控制器中放置多个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. 确保集合视图设置了正确的委托和数据源

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

问题是你正在使用"_collectionView",这可能是一些全局UICollectionView属性,但这个委托方法返回类型UICollectionView的"cv"

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

您需要始终检查委托或数据源中获得的collectionView实例。所以在你的情况下,而不是使用属性使用cv.tag_collectionView是一个属性(IBOutlet可能),它总是指向一个(希望)你的集合视图。

使你的代码更干净,我建议有单独的类,将作为委托/数据源为您的集合视图。在你的视图控制器中设置集合视图委托给那个单独的类而不是self。这样更容易维护。

试试下面的代码:

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

相关内容

  • 没有找到相关文章

最新更新