UICollectionView didSelectItem未调用

时间:2017-02-18 04:51:05

标签: ios objective-c uicollectionview

我使用一个简单的导航控制器,使用IB在其上添加了一个集合视图。我在标题中声明了集合视图的委托和数据源,并在IB中将其连接起来。集合视图填充所有图像,并将滚动,但从不调用didSelectItemAtIndexPath。发生了什么事?

抱歉,在点击提交之前忘了复制代码。

- (void)viewDidLoad
{

    [super viewDidLoad];

     arryData = [[NSArray alloc] initWithObjects:@"AIMCON", @"Devotional Songs", @"Saved Devotionals", @"Resources", @"Podcasts", @"Website", @"Links",  nil];
    CALayer * l = [viewofimage layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:11];
    [l setBorderWidth:2.0];
    [l setBorderColor:[[UIColor blackColor] CGColor]];
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [self.collectionView registerClass:[CVCell class] forCellWithReuseIdentifier:@"cvCell"];

        self.collectionView.backgroundColor = [UIColor blackColor];
    }
    else {
        [self.collectionView registerClass:[CVCell class] forCellWithReuseIdentifier:@"cvCell"];

        self.collectionView.backgroundColor = [UIColor blackColor];

    }


    // Do any additional setup after loading the view.
}





-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
    NSLog(@"1");
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [arryData count];
    NSLog(@"2");
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Selected");
    if (indexPath.row == 1) {
        DevoSongs *dvController = [[DevoSongs alloc] initWithNibName:@"DevoSongs" bundle:[NSBundle mainBundle]];

        [self.navigationController pushViewController:dvController animated:YES];
        [dvController release];
    }
    if (indexPath.row == 2) {
        ListOfDevos *dvController7 = [[ListOfDevos alloc] initWithNibName:@"ListOfDevos" bundle:[NSBundle mainBundle]];

        [self.navigationController pushViewController:dvController7 animated:YES];
        [dvController7 release];

    }
    if (indexPath.row == 3) {
        AIMProject *dvController7 = [[AIMProject alloc] initWithNibName:@"AIMProject" bundle:[NSBundle mainBundle]];

        [self.navigationController pushViewController:dvController7 animated:YES];
        [dvController7 release];

    }
    if (indexPath.row == 4) {
        Podcasts *dvController2 = [[Podcasts alloc] initWithNibName:@"Podcasts" bundle:[NSBundle mainBundle]];

        [self.navigationController pushViewController:dvController2 animated:YES];
        [dvController2 release];

    }
    if (indexPath.row == 5) {
        FamilyMinistry *dvController5 = [[FamilyMinistry alloc] initWithNibName:@"FamilyMinistry" bundle:[NSBundle mainBundle]];

        [self.navigationController pushViewController:dvController5 animated:YES];
        [dvController5 release];

    }
    if (indexPath.row == 6) {
        LinksViewController *dvController6 = [[LinksViewController alloc] initWithNibName:@"LinksViewController" bundle:[NSBundle mainBundle]];

        [self.navigationController pushViewController:dvController6 animated:YES];
        [dvController6 release];
    }

    if (indexPath.row == 0) {
        NSLog(@"TABLE");
        SECTable *dvController7 = [[SECTable alloc] initWithNibName:@"SECTable" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:dvController7 animated:YES];
        [dvController7 release];
    }

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



    static NSString *cellIdentifier = @"cvCell";

    CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];


    NSString *thearticleImage = [[arryData objectAtIndex:indexPath.row] stringByAppendingString:@".png"];
    [cell.theimage setImage:[UIImage imageNamed:thearticleImage]];
    //titleLabel2.text = entry.articleTitle;

    CALayer * l = [cell.theimage layer];
    [l setMasksToBounds:YES];
     [l setCornerRadius:11];
    [l setBorderWidth:2.0];
    [l setBorderColor:[[UIColor whiteColor] CGColor]];
    [cell.labels setText:[arryData objectAtIndex:indexPath.row]];

    return cell;
}

1 个答案:

答案 0 :(得分:5)

正如评论中所讨论的,由于没有为delegate设置collectionView,问题就出现了。添加行

self.collectionView.delegate = self

解决问题