Ios UICollectionView未调用didSelectItem
我使用一个简单的导航控制器,并使用IB在其上添加了一个集合视图。我在标题中声明集合视图的委托和数据源,并将其连接到IB中。集合视图将填充所有图像,并将滚动,但从未调用didSelectItemAtIndexPath。发生了什么事 抱歉,在单击“提交”之前忘记复制代码Ios UICollectionView未调用didSelectItem,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我使用一个简单的导航控制器,并使用IB在其上添加了一个集合视图。我在标题中声明集合视图的委托和数据源,并将其连接到IB中。集合视图将填充所有图像,并将滚动,但从未调用didSelectItemAtIndexPath。发生了什么事 抱歉,在单击“提交”之前忘记复制代码 - (void)viewDidLoad { [super viewDidLoad]; arryData = [[NSArray alloc] initWithObjects:@"AIMCON", @"Devot
- (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;
}
如评论中所述,出现问题的原因是未为
集合视图
设置委托
。添加行
self.collectionView.delegate = self
解决问题请提供您为集合视图@user717452My bad,@Sachin所做的代码。把它放进去now@user717452,您在哪里编写这些行?如果您没有设置您的
collectionView.delegate
,则该函数将不会被调用。我将其连接到IB中,以供委托使用,认为这就是所需的一切…将其添加到viewDidLoad中并成功,谢谢@KrishnaCA@user717452,我把它写成一本书answer@jbnunn,您需要将UICollectionView
的委托
设置为实现函数的类,否则将不会调用函数。这可以在故事板本身中完成,也可以在代码中完成