ios - 点击时更改 TableView 单元格的 UIImage

标签 ios objective-c uitableview uiimage uitapgesturerecognizer

可能是一个简单的修复。

我的表格 View Controller 的每个单元格内都有一个图像,上面有一个点击手势识别器。这工作正常并且记录正确。

我需要的是将图像从其默认状态更改为然后在“选定”状态(绿色)和“取消选定”状态(红色)之间切换。换句话说,它是一个一开始是灰色的 list ,然后你点击图像,图像变成绿色的勾号,再次点击,它变成红色的 x。

关键在于:当它只是 didSelectRowAtIndexPath 方法中的一个条件语句时,它是否有效,但由于我添加了手势识别器并创建了 imageTapped 方法,我似乎无法翻译过来。因此,其他有用的线程,如 this不要为我工作。

一如既往的感谢。你们是最棒的。

代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PlacesCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PlacesCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    //Create ImageView
    cell.imageView.image = [UIImage imageNamed:@"checkmark.png"];

    //Add Gesture Recognizer
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped)];
    tapped.numberOfTapsRequired = 1;
    [cell.imageView addGestureRecognizer:tapped];
    cell.imageView.userInteractionEnabled = YES;
    [cell addSubview:cell.imageView];

    return cell;

    }

//Method controlling what happens when cell's UIImage is tapped
-(void)imageTapped
{

    UITableViewCell *cell;

    UIImage *imageDefault = [UIImage imageNamed:@"checkmark.png"];
    UIImage *imageRed = [UIImage imageNamed:@"checkmark(red).png"];
    UIImage *imageGreen = [UIImage imageNamed:@"checkmark(green).png"];

    if (cell.imageView.image == imageDefault) {
        cell.imageView.image = imageGreen;
        cell.selected = true;
        NSLog(@"Selected");
    } else {
        cell.imageView.image = imageRed;
        cell.selected = false;
        NSLog(@"Deselected");
    }
}

最佳答案

我修改了你的代码,检查一下

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PlacesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PlacesCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

//Create ImageView
cell.imageView.image = [UIImage imageNamed:@"checkmark.png"];

//Add Gesture Recognizer
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];
cell.imageView.userInteractionEnabled = YES;
//[cell addSubview:cell.imageView]; 

return cell;

}

//Method controlling what happens when cell's UIImage is tapped
-(void)imageTapped:(UIGestureRecognizer*)gesture
{

UIImageView *selectedImageView=(UIImageView*)[gesture view];

UIImage *imageDefault = [UIImage imageNamed:@"checkmark.png"];
UIImage *imageRed = [UIImage imageNamed:@"checkmark(red).png"];
UIImage *imageGreen = [UIImage imageNamed:@"checkmark(green).png"];

if (selectedImageView.image == imageDefault) {
    selectedImageView.image = imageGreen;
    //cell.selected = true;
    NSLog(@"Selected");
} else {
    selectedImageView.image = imageRed;
    //cell.selected = false;
    NSLog(@"Deselected");
}
}

关于ios - 点击时更改 TableView 单元格的 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21838721/

相关文章:

ios - 在 iOS 上,如果没有 Apple Watch,如何收集锻炼时消耗的总能量?

objective-c - 如何在iOS中从字符串中切出字符串?

objective-c - 帮助我理解 obj-c 类类型

ios - MKMapCamera 无法缩放到正确的高度

ios - Theos实例方法调用

ios - 将 segue 从 UITableViewCell 推送到 Swift 中的 ViewController

ios - NSRangeException - 索引 (2008) 超出范围 (1)

ios - WKWebView嵌入UITableViewCell时,WebView的secondHalf不会显示

ios - 带有 Swift 3 的 APAddressBook - EXC_ARM_BREAKPOINT

ios - GPUImage - 尝试在混合过于频繁时过度释放帧缓冲区