これをフォローしようとしています:
-(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 つのカスタマイズされたアイテムが表示されますが、そのうちの 1 つを選択するとアプリがクラッシュします。( :(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;
}