UICollectionViewCell--查找cell上的按钮点击后,对应的是哪个cell

实际写项目会碰到各种各样的问题,废话不多说

按钮添加到cell时,根据是直接添加到self还是self.contentView上,在点击方法里找到btn的父视图

我是直接添加到self上,所以只有一层

- (void)btnClicked:(UIButton *)btn

{
  UIView *v =[btn superview];

   UICollectionViewCell *cell = (UICollectionViewCell *)v;

  

  NSIndexPath *tapIndexPath = [self.collectionView indexPathForCell:cell];

  NSLog(@"%li---%li",tapIndexPath.section,tapIndexPath.row);

}

若是代理,则直接将cell传过去便是

原文地址:https://www.cnblogs.com/hyuganatsu/p/UICollectionViewCell.html