我想从一个UIAlertController的细胞中显示一个UICollectionView。
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[alert addAction:deleteAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];问题是,单元格没有自表示presentViewController:警报动画:YES,completion:nil;方法。
也许有人能帮我?
发布于 2017-02-22 17:34:55
可以使用委托。
CollectionCell.h
#import <UIKit/UIKit.h>
@class CollectionCell;
@protocol CollectionCellDelegate
- (void)showDataFromCell:(CollectionCell *)cell;
@end
@interface CollectionCell : UICollectionViewCell
+ (NSString *)cellIdentifier;
@property (weak, nonatomic) id < CollectionCellDelegate > delegate;
@endCollectionCell.m
#import "CollectionCell.h"
@implementation CollectionCell
+ (NSString *)cellIdentifier {
return @"CollectionCell";
}
- (IBAction)buttonPressed:(UIButton *)sender {
[self.delegate showDataFromCell:self];
}
@endViewController.m
#import "ViewController.h"
#import "CollectionCell.h"
@interface ViewController () <CollectionCellDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - UITableView DataSource -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[CollectionCell cellIdentifier] forIndexPath:indexPath];
cell.delegate = self;
return cell;
}
- (void)showDataFromCell:(CollectionCell *)cell {
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
NSLog(@"Button pressed at cell with index: %ld", (long)indexPath.row);
}
@end发布于 2017-02-22 16:17:42
可以在viewController中创建包含集合视图的方法,并从单元格中调用该方法。就像这样:
- (void)presentAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[alert addAction:deleteAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
}然后从任何您喜欢的地方调用[self presentAlert]方法,即didSelectItemAtIndexPath
https://stackoverflow.com/questions/42396665
复制相似问题