如何在iphone的表格视图中更改图像

时间:2010-07-28 01:37:38

标签: iphone

我在表格单元格中有一个imageView现在我想要点击图像,如何做到这一点

 switch(indexPath.section)
case 0:  

    CGRect frame;


       frame= CGRectMake(20 ,10, 270, 180);//277 182
       UIImageView * myImageView = [[UIImageView alloc]init];
       myImageView.image = [UIImage imageNamed:@"Abe1.jpg"];// abe2, abe3.png


       myImageView.frame = frame;
       //UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bowl.png"]];
       [cell.contentView addSubview: myImageView];
       [myImageView release];

如何更改图像(abe1.png,abe2.png,abe3.png)??

1 个答案:

答案 0 :(得分:0)

您需要执行以下操作:

// This will check if the user had started to touch something
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    //Checks if the user touched your image.
    if([[touch view] == myImageView) {
        //Make a integer that will keep track of which image your on. Initialize at zero for Abe1.
        if(counter == 0) {
           myImageView.image = [UIImage imageNamed:@"Abe2.jpg"]; 
           counter = 1;
        }
        else if(counter == 1) {
           myImageView.image = [UIImage imageNamed:@"Abe3.jpg"]; 
           counter = 2;
        }
        else if(counter == 2) {
           myImageView.image = [UIImage imageNamed:@"Abe1.jpg"]; 
           counter = 0;
        }
    }
}

编辑:

抱歉,我忘了这是一张桌面视图。要检查哪一个是正确的单元格,请执行以下操作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];   
    if (row == 0){
        if(counter == 0) {
           myImageView.image = [UIImage imageNamed:@"Abe2.jpg"]; 
           counter = 1;
       }
        else if(counter == 1) {
           myImageView.image = [UIImage imageNamed:@"Abe3.jpg"]; 
           counter = 2;
       }
        else if(counter == 2) {
           myImageView.image = [UIImage imageNamed:@"Abe1.jpg"]; 
           counter = 0;
    }
}