我有我自己的TableViewCell
类,继承 UITableViewCell
。在我的单元格nib文件上,我在TableViewCell
中放置了一个图像。(图像没有完全占据整个单元格区域,图像周围有空格)
然后,我想实现一个触摸反馈功能,当用户触摸单元格中的图像时,图像将被另一个图像替换。
我试着做它在tableView:didSelectRowAtIndexPath:
方法:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//my TableViewCell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//New image to replace the current one
UIImage* bg = [CCTheme imageNamed:@"green_btn_bg"];
UIImageView* bgView = [[UIImageView alloc] initWithImage:bg];
cell.selectedBackgroundView = bgView;
...
但它根本不起作用。那么,我该如何执行这个触摸反馈功能呢?当用户的手指触摸到cell中的图像时,图像就会变成另一个。
在您的自定义UITableViewCell
类中创建UIImageView
属性作为IBOutlet
,然后从cellForRowAtIndexPath:
而不是背景属性中设置该属性上的图像。
cell.yourCustomImageView.image = bgView;
或者像下面这样将UIImageView添加到当前通用的UITableViewCell中。
显示当前单元格
[cell addSubview:bgView];
- 在
didSelectRowAtIndexPath:
中,您必须首先更改数据模型(指示单元格的状态已更改)。 - 您设置合适的图像
cellForRowAtIndexPath:
。(即您检查状态是什么并提供正确的图像。) - 更新cell,调用
reloadRowsAtIndexPaths:
。
您不应该将单元格的状态存储在单元格的某些视图中。例如,如果不同的图像代表某种选择,你的数组或其他数据结构,你的表视图应该跟踪哪一行是在这种状态。
每次必须重新生成单元格时,都调用cellForRowAtIndexPath
。(这可能是因为单元格变得可见,或者因为您显式地更新了它。)这是检查状态信息并相应地显示单元格的最佳位置。
我建议在您的自定义单元格的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"];
}
另外,不要忘记让你的自定义单元格声明像这样装饰:
@interface CustomTableViewCell : UITableViewCell <UIGestureRecognizerDelegate>
在cellForRowAtIndexPath:
方法中,您需要确保init方法中的代码正在被触发,以便添加tapRecognizer
。
祝你好运!
[编辑]
根据您如何使用自定义XIB创建单元格,您可能不需要这样做,但在我的情况下,我需要显式调用一个方法来初始化表单元格中UI的状态。我这样做的方式是在自定义表视图单元格上提供一个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;
}