首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按下自定义项UICollectionViewCell图像更改

按下自定义项UICollectionViewCell图像更改
EN

Stack Overflow用户
提问于 2013-01-03 13:42:48
回答 1查看 747关注 0票数 0

如何使细胞图像在长按压手势后发生变化?

在这里,当我点击一个单元格(长按压)时,会出现4个定制的项目,但当我选择其中一个时,应用程序就会崩溃。(如果删除:(单元格*)单元格和cell.imageView.image = [UIImage imageNamed:NSString NSString works:@“ICUbedRED.png”];它可以工作.我的意思是显示alertView,但图像当然不会改变)。

代码语言:javascript
复制
 - (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];



   }
}

空白处是:

代码语言:javascript
复制
 - (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"]];
.}

而构建单元的方法是:

代码语言:javascript
复制
- (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;


}
EN

回答 1

Stack Overflow用户

发布于 2013-01-25 09:22:20

@dottor自我感觉好极了

代码语言:javascript
复制
cell.imageView.image = [UIImage imageNamed:[NSString stringWi......

因为,对象被重新定义为param方法,如

  • (Void)lowDep:( Cell *)单元不属于类单元,所述的param属于UIMenuItem类。因为您单击的是menuItems,而不是单元格。

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

只需实现3个委托方法和

代码语言:javascript
复制
// 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。

希望这有帮助,原谅我的错误句子形成。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14140093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档