我会建议你自定义单元格的init方法中的钩手势识别为UIImageView
:
// Add recognizer for tappable image
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(imageViewTapped:)];
[tapRecognizer setNumberOfTouchesRequired:1];
[tapRecognizer setDelegate:self];
self.tappableImageView.userInteractionEnabled = YES;
[self.tappableImageView addGestureRecognizer:tapRecognizer];
然后处理程序:
- (void)imageViewTapped:(id)sender {
NSLog(@"image tapped");
self.tappableImageView.image = [UIImage imageNamed:@"some_different_image.png"];
}
另外,不要忘了让你自定义单元格声明装饰lik è这样:
@interface CustomTableViewCell : UITableViewCell <UIGestureRecognizerDelegate>
在cellForRowAtIndexPath:
方法,你需要确保你的init方法的代码被解雇,使得tapRecognizer
加入。
祝你好运!
[编辑]
取决于你如何使用你可能不需要这个自定义XIB创建你的,但对我来说,我需要显式调用方法来初始化用户界面的状态表格单元格。我这样做的方式是提供对自定义表视图细胞的initState
方法:它已被创建之后
- (void)initState {
// Do other things for state of the UI in table cell.
// Add recognizer for tappable image
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(imageViewTapped:)];
[tapRecognizer setNumberOfTouchesRequired:1];
[tapRecognizer setDelegate:self];
self.tappableImageView.userInteractionEnabled = YES;
[self.tappableImageView addGestureRecognizer:tapRecognizer];
}
然后在cellForRowAtIndexPath
我请确保调用initState
我的表格单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"CustomTableViewCell" bundle:nil];
// Grab a pointer to the custom cell.
cell = (CustomTableViewCell *)temporaryController.view;
[cell initState];
}
return cell;
}
待办事项你在'xib'中添加了'UIImageView'的插口?我猜你应该改变这个'imageView'的图像,而不是单元格的'selectedBackgroundView'属性 – 2013-04-25 14:13:44
是的,我有一个插座,你能更具体地了解在哪个函数下我应该改变插座图像吗? – Mellon 2013-04-25 14:16:39