如何在长按手势后更改单元格imageview?
当我点击一个单元格(longpress)时会显示4个自定义项目,但当我选择其中一个时,应用程序崩溃。 (如果删除:(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;
}
答案 0 :(得分:0)
@dottorfeelgood
崩溃了cell.imageView.image = [UIImage imageNamed:[NSString stringWi......
因为,该对象作为
等方法的param复活默认情况下,您可以使用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;
并在ViewdidLoad中设置sharedMenuController所需的自定义menuItem。
希望这有帮助,请原谅我的不良判决。