问题
I'm working on a custom layout collectionView which has quilt look and each section has a footer. Two buttons are inside footer and each button has a segue to another view(I set segues using IB).
Footer has its own indexPath which was assigned in a UICollectionViewFlowLayout class(Where I made a custom layout). The indexPath of footer is like this - {section - row} {0-0} {1-0} {2-0}....
I want to keep this indexPath information to use at the another view which is linked to two buttons.
I tried "convertPoint: toView: ... indexPathForRowAtPoint" way but didn't work. I think it's because the buttons are not belong to collectionViewItem but belong to Footer.
In this situation, what is the best way of passing each footerView's indexPath to segue?
Any solution with code example will be greatly appreciated.
// 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?
}
}
Below is header of Footer class
#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
回答1:
try the following:
imagine a reusable view with two buttons (leftButton and rightButton). in your reusable view classes .h file add a property for the indexpath:
@property (strong, nonatomic) NSIndexPath *indexPath;
then in your viewcontrollers viewForSupplementaryElementOfKind method:
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];
then implement the two methods:
- (void)leftButtonTapped:(UIButton *)sender {
[self performSegueWithIdentifier:@"LeftButtonSegue" sender:sender];
}
- (void)rightButtonTapped:(UIButton *)sender {
[self performSegueWithIdentifier:@"RightButtonSegue" sender:sender];
}
and finally in your viewcontrollers prepareForSegue method:
CustomFooterView *footerView = (CustomFooterView *)((UIButton *)sender).superview;
NSLog(@"perform segue from indexpath %ld, %ld", (long) footerView.indexPath.section, (long) footerView.indexPath.row);
IMPORTANT
the buttons have to be direct childs of the reusable view to make
(CustomFooterView *)((UIButton *)sender).superview
work. and of course you have to connect the segues LeftButtonSegue and RightButtonSegue in storyboard.
来源:https://stackoverflow.com/questions/29823846/how-to-pass-indexpath-from-button-inside-footer-to-segue