2013-04-25 48 views
1

我有我自己的TableViewCell类,其中继承UITableViewCell。在我的手机笔尖文件中,我已将图像放入我的TableViewCell。 (图像没有完全占据整个单元区域,图像周围有空格)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; 
    ... 

不过,这并不在所有的工作。那么,我该如何实现这种触摸反馈功能呢?当用户用手指触摸单元格中的图像时,图像会变为另一个图像。

+1

待办事项你在'xib'中添加了'UIImageView'的插口?我猜你应该改变这个'imageView'的图像,而不是单元格的'selectedBackgroundView'属性 – 2013-04-25 14:13:44

+0

是的,我有一个插座,你能更具体地了解在哪个函数下我应该改变插座图像吗? – Mellon 2013-04-25 14:16:39

回答

0
  • didSelectRowAtIndexPath:您必须首先更改您的数据模型(表明单元格状态已更改)。
  • 您可以设置合适的图片cellForRowAtIndexPath:。 (即,检查状态并提供正确的图像)。
  • 要更新单元格,请致电reloadRowsAtIndexPaths:

说明

您应该将单元的状态不存储在电池的一些看法。例如。如果不同的图像表示某种选择,那么提供您的表视图的数组或其他数据结构应该跟踪哪一行处于此状态。

每次必须重新生成单元时,调用cellForRowAtIndexPath。 (这可能是因为单元格变得可见,或者因为您明确更新了它。)这是检查状态信息并相应显示单元格的最佳位置。

+0

我只理解你的最后一点,前两个不清楚。你是什​​么意思和如何改变我的数据模型,你是什么意思关于设置适当的图像和如何?我确实已经设置了合适的图像。 – Mellon 2013-04-25 14:24:07

+0

请参阅我上面添加的解释。 – Mundi 2013-04-26 07:03:28

0

在您的自定义UITableViewCell类中创建一个UIImageView属性作为IBOutlet,然后从cellForRowAtIndexPath:而不是背景属性设置该属性上的图像。或者将UIImageView添加到当前的通用UITableViewCell(如下所示)。

与您当前单元格

[cell addSubview:bgView]; 
0

我会建议你自定义单元格的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; 

} 
+0

谢谢,但如何确保在cellForRowAtIndexPath方法init方法被解雇? – Mellon 2013-04-26 07:14:12

+0

@Mellon,我添加了一些代码,显示了如何使用自定义XIB并在我的单元上调用自定义的'initState'方法来挂接手势识别器。 – Aaron 2013-04-26 15:12:01