how to change image on tap in table view for iphone
i have a imageView in tab开发者_StackOverflow社区le 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)??
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;
}
}
精彩评论