长按自定义项 UICollectionViewCell 图像更改

Posted

技术标签:

【中文标题】长按自定义项 UICollectionViewCell 图像更改【英文标题】:longpress custom items UICollectionViewCell image change 【发布时间】:2013-01-03 13:42:48 【问题描述】:

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

当我点击一个单元格(长按)时,会出现 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;



【问题讨论】:

【参考方案1】:

@dottorfeelgood 它正在崩溃

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

因为,对象作为参数返回给像

这样的方法 (void)lowDep:(Cell*)cell 不属于 Cell 类,返回的参数属于 UIMenuItem 类。因为您点击的是 menuItems 而不是 Cell。

你可以使用 UICollectionView 默认提供的 UICollectionCell 解决方案上的 MenuItems 和相应的操作,而不是做你现在正在做的事情。您可以查看本教程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;

并将所需的自定义 menuItems 设置为 ViewdidLoad 中的 sharedMenuController。

希望对你有帮助,请原谅我的造句不当。

【讨论】:

以上是关于长按自定义项 UICollectionViewCell 图像更改的主要内容,如果未能解决你的问题,请参考以下文章

使用 QStyledItemDelegates 作为 QListView 中的自定义项

带有自定义项目的 QListView => 鼠标事件被传播

带有自定义项的 QTreeView

带有自定义项的 GWT 组合框

意图选择器屏幕中自定义项目的回调

在超集中构建自定义马条形图并添加自定义项以自定义图表