【问题标题】:Change Table View Cell's UIImage on Tap点击更改表格视图单元格的 UIImage
【发布时间】:2014-02-17 20:31:44
【问题描述】:

可能是一个简单的修复。

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

我需要将图像从其默认状态更改为在“选中”状态(绿色)和“取消选中”状态(红色)之间切换。换句话说,它是一个清单,一开始是灰色的,然后你点击图像,图像变成绿色复选标记,再次点击,它变成红色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");
    }
}

【问题讨论】:

    标签: ios objective-c uitableview uiimage uitapgesturerecognizer


    【解决方案1】:

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

    - (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");
    }
    }
    

    【讨论】:

    • 完美运行!谢谢@pawan!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多