objective-c - UIActionSheet 只显示一次

标签 objective-c ios uiactionsheet uicollectionview

我有以下生成 UICollectionView 的代码。当您长按 UICollectionViewCell 时,会出现一个 UIActionSheet。

这有效,但只有一次。如果您关闭 UIActionSheet 并再次长按同一个单元格,则不会发生任何事情。

知道我做错了什么吗?

#import "ProjectsListViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ProjectsListViewController ()

@end

@implementation ProjectsListViewController

@synthesize appDelegate;

- (void)viewDidLoad
{
    [super viewDidLoad];

    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    // CREATE THE COLLECTION VIEW
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    [self setCollectionView:[[UICollectionView alloc] initWithFrame:CGRectMake(20, 54, [[self view] bounds].size.width - 40, [[self view] bounds].size.height) collectionViewLayout:flowLayout]];
    [[self collectionView] setDataSource:self];
    [[self collectionView] setDelegate:self];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    [[self collectionView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
}

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
    return [[[appDelegate userSettingsDictionary] objectForKey:@"Projects"] count];
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView 
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    // CREATE A BACKGROUND VIEW FOR THE FOLDER IMAGE
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"folder.png"]];
    UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 122, 89)];
    [background setBackgroundColor:[UIColor colorWithPatternImage:image]];
    [cell addSubview:background];

    // SET THE CELL TEXT
    UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 95, 130, 100)];
    [cellLabel setText:[[[[[appDelegate userSettingsDictionary] objectForKey:@"Projects"] objectAtIndex:[indexPath row]] objectForKey:@"Project Name"] uppercaseString]];

    // LISTEN FOR A LONG PRESS ON EACH FOLDER
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    [cell addGestureRecognizer:longPress];

    [cell addSubview:cellLabel];
    return cell;
}

// PRESENT AN ACTION SHEET WHEN A FOLDER HAS RECEIVED A LONG PRESS EVENT
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer
{
    if ( [recognizer state] == UIGestureRecognizerStateBegan )
    {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select an action" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:@"Edit", nil];

        [actionSheet addGestureRecognizer:recognizer];

        // SET THE SELECTED FOLDER'S ROW NUMBER AS THE ACTIONSHEET TAG.  JUST A WAY OF LETTING THE DELETE METHOD KNOW WHICH FOLDER TO DELETE
        [actionSheet showInView:self.view];
    } 
}

// GET THE SIZE OF THE FOLDER IMAGE AND SET EACH COLLECTION VIEW ITEM SIZE TO FIT
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"folder.png"]];
    return CGSizeMake(image.size.width, image.size.height + 50);
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 90, 10);
}

@end

谢谢

最佳答案

实际上我不久前尝试过类似的东西,发现几乎整个方法都是错误的。

首先:您设置单元格的方式不好。因为单元格是可重用的,但是现在每次 Collection View 请求特定单元格时您都添加 subview 。您应该子类化 UICollectionViewCell 并在 initWithCoder: 方法中添加 subview 。

然后只需在文本字段的子类上创建一个属性。然后在 collectionView:cellForItemAtIndexPath: 中,您只需将文本设置为标签 text 属性。

现在让我们修复手势识别器。您不应该为每个 UICollectionViewCell 设置一个手势识别器,而应该只设置一个全局的。在 viewDidLoad: 中你应该添加:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[self.collectionView addGestureRecognizer:longPress];

那么您应该将 handleLongPress: 更改为如下内容:

- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
        NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[gesture locationInView:self.collectionView]];

        if (indexPath != nil) {
            self.currentIndexPath = indexPath;

            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select an action" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:@"Edit", nil];

            UICollectionViewCell *itemCell = [self.collectionView cellForItemAtIndexPath:indexPath];
            [action showFromRect:CGRectMake(0, 0, itemCell.frame.size.width, itemCell.frame.size.height) inView:itemCell animated:YES];
        }
    }
}

请注意,我没有在操作表中添加用于添加识别器的行,我不明白您为什么要这样做。您还应该添加一个名为 currentIndexPath 的属性。

一切都应该在这一刻准备就绪。当您从操作表中获得响应时,只需使用 self.currentIndexPath 来确定要删除/编辑的项目。

关于objective-c - UIActionSheet 只显示一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12837772/

相关文章:

ios - 如何在 iOS 7(越狱)中获取 SpringBoard 的任务端口?

ios - 为什么 App 名称更改没有反射(reflect)在 iOS 7 通知中心?

ios - 如何在渲染后重用 map View

ios - 选择指示器未显示在操作表内的选择器 View 中

iphone - 如何自定义UIActionSheet中的按钮?

iphone - iOS 开发 : How to change text in the textfield?

ios - 如何告诉 Xcode 如何包含用尖括号指定的库?

ios - 使用数组更改 UITextField 的值不起作用

ios - Swift 自动续订订阅问题

ios - Xcode4.3 UIActionSheet 导致错误消息