在Tap上更改Table View Cell的UIImage

时间:2014-02-17 20:31:45

标签: ios objective-c uitableview uiimage uitapgesturerecognizer

可能只是一个简单的修复。

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

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

1 个答案:

答案 0 :(得分:4)

我修改了你的代码,检查它

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