我对此感到震惊了一段时间。我的错误是时间表中的一些单元格didSelectItemAtIndexPath没有调用。我在方法上保留了一个断点,当我点击一些单元格时,方法没有被调用。可能是什么问题呢。请参阅以下代码
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
return 25;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *) collectionView
{
return 1;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"item clicked");
CGRect rect = [collectionData layoutAttributesForItemAtIndexPath:indexPath].frame;
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController* vc = [storyboard instantiateViewControllerWithIdentifier:@"note"];
pc = [[UIPopoverController alloc] initWithContentViewController:vc];
[pc presentPopoverFromRect:rect inView:collectionData
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
NSLog(@" The index path is %@", indexPath);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// we're going to use a custom UICollectionViewCell, which will hold an image and its label
Cell *myCell = [collectionData dequeueReusableCellWithReuseIdentifier:@"TableCell"
forIndexPath:indexPath];
if (indexPath.row < 20 || indexPath.row == 22 )
{
myCell.roomLabel.text = [NSString stringWithFormat:@"{%ld,%ld}", (long)indexPath.row, (long)indexPath.section];
NSLog(@"cell no %ld" ,(long)indexPath.row );
myCell.subjectLabel.text =@"English";
myCell.cellView.backgroundColor= [UIColor grayColor];
[myCell.redXbuttonOutlet addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
if(isLongPressed)
{
[myCell.redXbuttonOutlet setHidden:NO];
}
else
{
[myCell.redXbuttonOutlet setHidden:YES];
}
}
else
{
myCell.userInteractionEnabled = NO;
[myCell.redXbuttonOutlet setHidden:YES];
myCell.cellView.backgroundColor= [UIColor whiteColor];
myCell.subjectLabel.text =@"";
myCell.roomLabel.text = @"";
}
return myCell;
}
-(void)buttonPressed:(id)sender
{
NSLog(@"Button");
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Delete session" message:@"Do you want to delete this session" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
答案 0 :(得分:4)
您已禁用某些单元格的用户交互。 检查下面的代码,它位于cellForItemAtIndexPath
中else
{
myCell.userInteractionEnabled = NO; // <-----Note this Line
[myCell.redXbuttonOutlet setHidden:YES];
myCell.cellView.backgroundColor= [UIColor whiteColor];
myCell.subjectLabel.text =@"";
myCell.roomLabel.text = @"";
}