1

试图遵循这个:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath//tableview delegate
{
    //here is the code to create and customize a cell
    //adding gesture recognizer
    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc]
    initWithTarget:self action:@selector(your_function)];
    recognizer.minimumPressDuration = 1.0; //seconds
    [cell addGestureRecognizer:lpgr];
    [lpgr release];
    return  cell;
}

-(void)your_function
{
    NSlog(@"detecting long press");
}

这是我的问题:

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

我想我需要在-(void)your_function {}.

问题更新

真正使用它:

有了这个,当我单击一个单元格(长按)时,会出现 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
 {...}

 - (void)free:(Cell*)cell
 {...}

 - (void)booked:(Cell*)cell
 {...}

细胞构建方法是:

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

-1

将方法更改为:

-(void)your_function:(UITableViewCell*)cell
{
    cell.imageView = .....
}

识别器会将单元格作为印刷机的发送者发送,并将其传递给您的方法。

注意您必须将识别器更改为:

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

添加:到方法的末尾,因为您的方法有一个参数。

于 2012-12-31T19:16:04.127 回答