iPad中的单个视图控制器中有多个UICOLLECTIONView

时间:2014-07-09 06:00:47

标签: ios objective-c ipad uicollectionviewcell uicollectionreusableview

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

以上代码正在运行,但它仅适用于一个集合视图我使用断点调试它,因此它进入第一个return语句。所以代码的另一部分甚至没有运行。所以如何解决这个问题。 提前谢谢你:)

4 个答案:

答案 0 :(得分: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;
        } 
    }
    

答案 1 :(得分:0)

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

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

答案 2 :(得分: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;
}

此代码适用于您。

答案 3 :(得分:0)

问题是您正在使用"_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;
} }