【发布时间】:2015-10-18 16:49:57
【问题描述】:
我都经历过
How to get the selected CollectionViewCell in the UICollectionView?
how to access from UICollectionViewCell the indexPath of the Cell in UICollectionView
但是我的问题和上面两个有点不同。
我在 mybookingsCell 上有来自 nearByCollectionView 的评论按钮
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
DetailObject * Obj=[[DetailObject alloc]init];
Obj=[self.listOfObjects objectAtIndex:indexPath.row];
// DetailObject is NSObject class and listOfObjects getting values from API
// mybookingsCell.headerLabel.text=Obj.name; like this i am putting the values in cell
NearbyCell *mybookingsCell = (NearbyCell *)[cv dequeueReusableCellWithReuseIdentifier:@"nearby" forIndexPath:indexPath];
[mybookingsCell.reviewBtn addTarget:self action:@selector(didTapReviewBtn:) forControlEvents:UIControlEventTouchDown];
}
-(IBAction)didTapReviewBtn:(id)sender
{
}
点击查看按钮时,我需要单元格上的详细信息。 我试过这样
-(IBAction)didTapReviewBtn:(id)sender
{
NearbyCell *clickedCell = (NearbyCell *)[[sender superview] superview];
NSIndexPath *clickedButtonIndexPath = [self.nearByCollectionView indexPathForCell:clickedCell];
}
但是我的应用程序崩溃了。所以帮助我如何通过相应的点击按钮来获取单元格详细信息。
【问题讨论】:
-
UITableView和UICollectionView都支持重复中提到的这种行为。 -
删除
collectionView: cellForItemAtIndexPath:中DetailObject的alloc/init。如果应用程序崩溃,您应该会收到一条错误消息。您是否检查过 [[sender superView] superview] 确实是NearByCell对象? -
@iosDev82 正确阅读问题stackoverflow.com/questions/7504421/…,然后再发表评论。他在说“问题是我不知道如何知道在哪个单元格上按下了按钮。”我的不一样。不要轻易投反对票
-
@ShrikantKankatti 请通过我发布的链接查看此答案stackoverflow.com/a/16270198/1891327。我知道您的问题可以通过这种方式解决。并且不要使用
superView.superView,因为它可能因单元格而异。 -
@iosDev82 我知道这是错误的,这就是我来到这里的原因,把我尝试过的代码放在这里。
标签: ios objective-c uibutton uicollectionview uicollectionviewcell