0

如何在长按手势后更改单元格图像视图?

有了这个,当我单击一个单元格(长按)时,会出现 4 个自定义项目,但是当我选择其中一个时,应用程序会崩溃。(如果您删除 :(Cell*)cell 和 cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"ICUbedRED.png"]]; 它可以工作...我的意思是出现 alertView 但图像当然不会'不改变)。

 - (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;


}
4

1 回答 1

0

@dottorfeelgood 它正在崩溃

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

因为,对象作为参数返回给方法,如

  • (void)lowDep:(Cell*)cell 不属于 Cell 类,返回的参数属于 UIMenuItem 类。因为您单击的是 menuItems 而不是 Cell。

您可以在 UICollectionView 默认提供的 UICollectionCell 解决方案上使用 MenuItems 和相应的操作,而不是做您现在正在做的事情。你可以在这里查看本教程!

只需实现 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。

希望这会有所帮助,请原谅我的不良句子形成。

于 2013-01-25T09:22:20.753 回答