如何从页脚内的button传递indexPath继续?

我正在做一个自定义布局collectionView,它有被子看,每个部分都有一个页脚。 两个button是在页脚内,每个button都有一个segue到另一个视图(我使用IB设置segues)。

页脚有它自己的indexPath在UICollectionViewFlowLayout类(我在哪里做了一个自定义的布局)分配。 footer的indexPath是这样的 – {section – row} {0-0} {1-0} {2-0} ….

我想保持这个indexPath信息在另一个链接到两个button的视图使用。

我试过“convertPoint:toView:… indexPathForRowAtPoint”的方式,但没有奏效。 我认为这是因为button不属于collectionViewItem,但属于页脚。

在这种情况下,传递每个footerView的indexPath的最好方法是什么?

代码示例的任何解决scheme将不胜感激。

// CollectionView DataSource - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { Footer *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath]; // set button images and labels [footerview setButton]; // debug code : show current section number footerview.footerLabel.text = [NSString stringWithFormat:@"Section %li", (long)indexPath.section]; return footerview; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if([[segue identifier] isEqualToString:@"SeeAllPictures"]) { // how to pass indextPath? } else if([[segue identifier] isEqualToString:@"GoDiary"]) { // how to pass indextPath? } } 

以下是Footer类的标题

 #import <UIKit/UIKit.h> @interface Footer : UICollectionReusableView @property (strong, nonatomic) IBOutlet UILabel *footerLabel; @property (strong, nonatomic) IBOutlet UIButton *seeAllPictures; @property (strong, nonatomic) IBOutlet UIButton *goDiary; @property (strong, nonatomic) IBOutlet UIImageView *seeAllPicturesImage; @property (strong, nonatomic) IBOutlet UILabel *seeAllPicturesLabel; @property (strong, nonatomic) IBOutlet UIImageView *goDiaryImage; @property (strong, nonatomic) IBOutlet UILabel *goDiaryLabel; - (void)setButton; @end 

尝试以下方法:

想象一个带有两个button(leftButton和rightButton)的可重用视图。 在你的可重用视图类.h文件中添加一个indexpath属性:

 @property (strong, nonatomic) NSIndexPath *indexPath; 

然后在你的viewcontrollers viewForSupplementaryElementOfKind方法中:

 CustomFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath]; footerView.indexPath = indexPath; [footerView.leftButton addTarget:self action:@selector(leftButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; [footerView.rightButton addTarget:self action:@selector(rightButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 

然后执行两个方法:

 - (void)leftButtonTapped:(UIButton *)sender { [self performSegueWithIdentifier:@"LeftButtonSegue" sender:sender]; } - (void)rightButtonTapped:(UIButton *)sender { [self performSegueWithIdentifier:@"RightButtonSegue" sender:sender]; } 

最后在你的viewcontrollers prepareForSegue方法中:

 CustomFooterView *footerView = (CustomFooterView *)((UIButton *)sender).superview; NSLog(@"perform segue from indexpath %ld, %ld", (long) footerView.indexPath.section, (long) footerView.indexPath.row); 

重要

button必须是可重用视图的直接子对象

 (CustomFooterView *)((UIButton *)sender).superview 

工作。 当然你必须连接故事板上的LeftButtonSegueRightButtonSegue