来自 UICollectionViewCell 的 UIAlertController
UIAlertController from UICollectionViewCell
我想从 UICollectionView 的 Cell 中显示一个 UIAlertController。
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 presentViewController:alert animated:YES completion:nil];方法。
也许有人可以帮助我?
您可以在 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
您可以使用委托
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;
@end
CollectionCell.m
#import "CollectionCell.h"
@implementation CollectionCell
+ (NSString *)cellIdentifier {
return @"CollectionCell";
}
- (IBAction)buttonPressed:(UIButton *)sender {
[self.delegate showDataFromCell:self];
}
@end
ViewController.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
我想从 UICollectionView 的 Cell 中显示一个 UIAlertController。
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 presentViewController:alert animated:YES completion:nil];方法。
也许有人可以帮助我?
您可以在 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
您可以使用委托
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;
@end
CollectionCell.m
#import "CollectionCell.h"
@implementation CollectionCell
+ (NSString *)cellIdentifier {
return @"CollectionCell";
}
- (IBAction)buttonPressed:(UIButton *)sender {
[self.delegate showDataFromCell:self];
}
@end
ViewController.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