【发布时间】:2014-11-12 05:02:03
【问题描述】:
我有一个 UIViewController,其中有一个 UICollectionView 以编程方式在其中创建的图库。我想在 UICollectionView 单元格的每个 uiimage 上添加一个按钮:CMFViewController.h.m 文件中用于向 imageview 添加按钮的代码是:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CMFGalleryCell *cell = (CMFGalleryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
NSString *imageName = [self.dataArray objectAtIndex:indexPath.row];
[cell setImageName:imageName];
// [[cell myButton] addTarget:self action:@selector(myClickEvent:event) forControlEvents:UIControlEventTouchUpInside];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 210.0, 100.0, 20.0);
[button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"ShowView" forState:UIControlStateNormal];
// NSMutableArray *buttonArray = [NSMutableArray arrayWithCapacity:100];
// for (int i = 0; i < 4; i++) {
// NSUInteger index = arc4random() % 4;
// newButton.frame = CGRectMake( 5, 5, 10, 10); // Whatever position and size you need...
// UIImage *buttonImage = [UIImage imageNamed:[self.dataArray objectAtIndex:indexPath.row]];
//[newButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
// [buttonArray addObject:newButton];
// }
// newButton = buttonArray; // Where myButtons is a NSArray property of your class, to store references to the buttons.
// [newButton addTarget:self action:@selector(loadImages:) forControlEvents:UIControlEventTouchUpInside];
//[newButton addTarget:self action:@selector(updateCell) forControlEvents:UIControlEventTouchUpInside];
[cell updateCell];
return cell;
}
我已通过以下链接从代码中获得帮助 adding-a-uibutton-to-a-uicollectionview
UIButton in cell in collection view not receiving touch up inside event 请给我你的建议并帮助我解决这个问题。
从这个链接你可以在Creating a Paged Photo Gallery With a UICollectionView 中下载source code:他们我想将下一个和上一个按钮放在屏幕每个图像的中间行
【问题讨论】:
-
向单元格添加按钮。
[cell addSubview: button]; -
请在源代码上尝试在每个图像上放置下一个和上一个按钮。我试过 [cell addSubview: button];但它不工作..
-
只需在单元格的 xib 中执行此操作即可。拖出两个按钮并将它们放在图像视图上您想要的位置。给他们 centerY 约束,以及你想要的与图像视图两侧的距离。
-
按钮显示在单元格中。我检查源并添加按钮。但如果您想使用下一个以前的功能,您也可以在
CMFViewController.xib上添加按钮。你也可以试试@rdelmar 评论。 -
@Wwwwwwwwwwwww 我编辑了代码检查它
标签: ios objective-c iphone uibutton uiimage