tags:

views:

54

answers:

1

i have a imageView in table cell now i want to chnage image on tap how to do that

 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];

how to change image on tap ( abe1.png,abe2.png,abe3.png)??

A: 

You need to do the following following:

// 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;
        }
    }
}

EDIT:

Sorry I forgot it was a tableview. To check which one is the correct cell do the following:

- (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;
    }
}
thyrgle
Why is your touchesBegan not working? What error is it giving?
thyrgle
You need to override the method.
thyrgle
Just copy and paste the method. Then make a counter in the interface.
thyrgle
Hmmm... Is the image full screen? If so you can delete that extra condition.
thyrgle
You might want to check if the user taps the cell then and use the rest of my logic.
thyrgle
Did you get it solved then since you accepted my answer?
thyrgle
Ok, edited the answer.
thyrgle
no still i cant override touch method and i am in section view not row i did this if (indexPath.row == 0 ) { NSLog(@"sdsdfs"); } now ??
ram
Don't overide touchesBegan override the tableView method.
thyrgle
now its working but its working on all cells when ever i touch even if upper cells then also image is changing
ram
Did you take away the touches began? Just checking.
thyrgle
it dosent matter whether touch-began is there or not i tried both ways(with n without) but its cell issues any idea
ram
wowthanks a lotin my case it was fucking section NSUInteger sections = [indexPath section]; NSLog(@"row is %d",sections);
ram
awsome thanks to you who suppored me all the way :-)
ram