我有一个 Collection View [不是自定义 Collection View ],我想通过单击按钮显示一个警报。但是按钮 Action 没有检测到我的代码的 Action :
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [ self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSArray *sectionArr=[self.ClassesArr objectAtIndex:indexPath.section];
NSDictionary *data = [sectionArr objectAtIndex:indexPath.row];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(54, 25, 15, 15);
[btn addTarget:self action:@selector(subCateBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
}
// the button action
-(IBAction)subCateBtnAction:(UIButton *)btn
{
NSArray *sectionArr=[self.ClassesArr objectAtIndex:sectionindex];
NSDictionary *data = [sectionArr objectAtIndex:btn.tag];
NSString *name = [data objectForKey:@"nombreTarifa"];
UIAlertView *Notpermitted=[[UIAlertView alloc] initWithTitle:@""
message:name
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[Notpermitted show];
}
可能是什么问题谢谢!
最佳答案
看起来您没有将按钮添加到单元格或返回单元格,试试这个:
[cell addSubview:btn];
return cell;
但是您是否意识到 UICollectionView 有一个用于选择的委托(delegate)方法? collectionView:didSelectItemAtIndexPath:
关于ios - 无法检测按钮 Action Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21222253/