在tableviewcell中點擊imageview時,我必須撥打特定的號碼。呼叫的號碼顯示在imageview旁邊的標籤中。但我無法獲取哪個單元格被點擊總是指最後一個單元格。在UITableViewCell中檢測UIImageView點擊
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(cell == nil)
{
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
lblphone = [[UILabel alloc] initWithFrame:CGRectZero];
lblphone.tag = 116;
lblphone.backgroundColor = [UIColor clearColor];
[lblphone setFont:[UIFont fontWithName:@"Helvetica" size:12]];
[lblphone setLineBreakMode:UILineBreakModeWordWrap];
[lblphone setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelButton:)];
tapGestureRecognizer.cancelsTouchesInView=NO;
[tapGestureRecognizer setNumberOfTapsRequired:1];
[lblphone addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
[cell addSubview:lblphone];
myImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
myImageView.tag = 120;
myImageView.image=[UIImage imageNamed:@"CallImg.png"];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageSelectedInTable:)];
[tapped setNumberOfTapsRequired:1];
[myImageView addGestureRecognizer:tapped];
[tapped release];
[cell addSubview:myImageView];
}
[myImageView setFrame:CGRectMake(10, 50,30,30)];
myImageView= (UIImageView*)[cell viewWithTag:120];
myImageView.image=[UIImage imageNamed:@"CallImg.png"];
myImageView.userInteractionEnabled=YES;
CGSize constraint5 = CGSizeMake(320, 2000.0f);
CGSize size5=[phone sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
lblphone =(UILabel *)[cell viewWithTag:116];
[lblphone setFrame:CGRectMake(45,name.frame.size.height+name.frame.origin.y,320, size5.height)];
lblphone.textAlignment=UITextAlignmentLeft;
lblphone.backgroundColor=[UIColor clearColor];
lblphone.text=[NSString stringWithFormat:@"%@ ",phone ];
[lblphone sizeToFit];
}
而在imageclick的tapgesture我寫這:
-(IBAction)imageSelectedInTable:(UITapGestureRecognizer*)gesture
{
UIImageView *imgView = (UIImageView *) [gesture view];
UITableViewCell *cell = (UITableViewCell*)[[imgView superview]superview];
NSIndexPath *tappedIndexPath = [self.tableView indexPathForCell:cell];
NSLog(@"Image Tap %@", tappedIndexPath);
UILabel *phoneno = (UILabel *)[cell viewWithTag:116];
NSString *phoneNumber = [@"telprompt://" stringByAppendingString:phoneno.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
}
但它始終是referncing到最後一個單元格點擊無論哪個小區clicked.Couldn't明白的地方我去錯了?
在imageView上創建一個自定義按鈕,並通過觸摸方法找到索引路徑,因此無需在代碼中使用UITapGestureRecognizer。 – Apple 2013-04-05 06:50:44