2012-10-11 64 views
0

我有下面的代碼,它產生一個UICollectionView。當您在UICollectionViewCell上長按時,會出現UIActionSheet。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 

感謝

回答

5

我前段時間試過類似的東西,發現幾乎整個方法都是錯誤的。

開始:你設置你的細胞的方式不好。因爲單元格是可重用的,但現在每次收集視圖要求特定單元格時添加子視圖。您應該子類UICollectionViewCell並在initWithCoder:方法中添加子視圖。

然後只是在文本字段的子類上創建一個屬性。然後在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來確定要刪除/編輯的項目。

+0

真是個好回覆。這解決了我的問題,以及我遇到的其他問題。非常感謝您的幫助。 – Typhoon101

1

handleLongPress:您指定的手勢識別的動作片。

[actionSheet addGestureRecognizer:recognizer]; 

我不知道你爲什麼這麼做,但手勢識別器只能用於單個視圖。此分配可能會從表格視圖單元格中移除手勢識別器,以便表格單元格上的長按手勢不再起作用。