0

如何在longpress手勢後更改單元格圖像視圖?longpress自定義項目UICollectionViewCell圖像更改

當我點擊一個單元格(longpress)時會出現4個自定義項目,但是當我選擇其中一個應用程序時會崩潰。 (如果你刪除:(單元格*)單元格和cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@「ICUbedRED.png」]];它的工作原理...我的意思是alertView出現,但當然圖像doesn不會改變)。

- (void)longPress:(UILongPressGestureRecognizer *)recognizer { 
    if (recognizer.state == UIGestureRecognizerStateBegan) { 
     Cell *cell = (Cell *)recognizer.view; 
     [cell becomeFirstResponder]; 

     UIMenuItem *highDep = [[UIMenuItem alloc] initWithTitle:@"High Dependency" action:@selector(hiDep:)]; 
     UIMenuItem *lowDep = [[UIMenuItem alloc] initWithTitle:@"Low Dependency" action:@selector(lowDep:)]; 
     UIMenuItem *booked = [[UIMenuItem alloc] initWithTitle:@"Booked"  action:@selector(booked:)]; 
     UIMenuItem *free = [[UIMenuItem alloc] initWithTitle:@"Free" action:@selector(free:)]; 

      UIMenuController *menu = [UIMenuController sharedMenuController]; 
    [menu setMenuItems:[NSArray arrayWithObjects:booked, highDep, lowDep, free, nil]]; 
     [menu setTargetRect:cell.frame inView:cell.superview]; 
     [menu setMenuVisible:YES animated:YES]; 



    } 
} 

空隙是:

- (void)hiDep:(Cell*)cell 
{ 
    NSLog(@"Bed is HiDep"); 

    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"ICUbedRED.png"]]; 




    UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:@"This Bed is High  Dependency" 
               message:@"" 
               delegate:self cancelButtonTitle:@"OK"  otherButtonTitles:nil, nil]; 



    [testAlert show]; 
    [testAlert release]; 


    } 

- (void)lowDep:(Cell*)cell 
{. 
    cell.imageView.image = [UIImage imageNamed:[NSString  stringWithFormat:@"ICUbedYELLOW.png"]]; 
..} 

- (void)free:(Cell*)cell 
{.. 
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"ICUbedGREEN.png"]]; 
.} 

- (void)booked:(Cell*)cell 
{.. 
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"ICUbedBLUE.png"]]; 
.} 

和單元建築的方法是:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView  cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *identifier = @"Cell"; 
    Cell *cvc = (Cell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    int i = indexPath.row%[labelArray count]; 
    number = i; 

    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; 



    [cvc addGestureRecognizer:recognizer]; 


    cvc.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"icubed.png"]]; 
    cvc.label.text = [labelArray objectAtIndex:number]; 

     return cvc; 


} 

回答

0

@dottorfeelgood據崩潰爲

cell.imageView.image = [UIImage imageNamed:[NSString stringWi...... 

因爲,對象retured作爲方法l的參數IKE

  • (無效)lowDep:(小區*)細胞 是類細胞不是,則retured參數是類UIMenuItem的。因爲你點擊menuItems而不是Cell。

而不是做你現在正在做什麼的,你可以使用的MenuItems和相應的動作在默認情況下由UICollectionView提供UICollectionCell解決方案。你可以查看這個教程here

只實現了3種委託方法和

// These methods provide support for copy/paste actions on cells. 
// All three should be implemented if any are. 
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath; 
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender; 
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender; 

,並設置需要在viewDidLoad中的sharedMenuController您的自定義的菜單項。

希望這會有所幫助,請原諒我的壞句子形成。

相關問題