2013-04-05 76 views
2

在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明白的地方我去錯了?

+0

在imageView上創建一個自定義按鈕,並通過觸摸方法找到索引路徑,因此無需在代碼中使用UITapGestureRecognizer。 – Apple 2013-04-05 06:50:44

回答

3

我看到您嘗試訪問您的細胞,

UITableViewCell *cell = (UITableViewCell*)[[imgView superview]superview]; 

但您將圖像視圖添加到

[cell addSubview:myImageView]; 

您應該將圖片視圖添加到cell.contentView

+0

這是一個非常可行的替代方法,使用標籤存儲行索引作爲@iPatel建議。有兩個明顯的缺點,但你需要考慮。 1)你不能在包含章節的tableview中使用它。 2)你放棄了使用標籤來訪問單元格contentView中的不同UI元素的能力。 – svena 2013-04-05 07:59:24

1

用於獲取/知道是單擊的單元格,寫入下面的代碼

UITableViewCell *cell = (UITableViewCell *)[[imgView superview]superview]; 

,如果你也想挖細胞的indexPath然後寫

NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 

編輯: - >另一種方式是

tagUIImageViewcellForRowAtIndexPath方法 諸如此類,

imageView.tag = indexPath.row; 

而且使用下面的代碼

int row = ((UIImageView*)imgView).tag; //Or// int row = ((UIGestureRecognizer *)sender).view.tag; 

NSIndexPath* indexpath = [NSIndexPath indexPathForRow:row inSection:0]; 
UITableViewCell* cell = [table cellForRowAtIndexPath:indexPath]; 

在這裏你可以得到螺紋UIImageViewCell

+0

但在我的情況下,NSIndexpath變爲null。我該怎麼辦? – Aaradhya 2013-04-05 06:23:37

+1

@ Aaradhya-試用這段代碼:) – iPatel 2013-04-05 06:30:49

+0

但是我已經在我的代碼中寫過它了。可以告訴我,我寫的和我的代碼有什麼區別? – Aaradhya 2013-04-05 06:37:26

3

它看起來像圖像視圖是單元格的一個子視圖(樹下一步),並且正在探測父代的代碼正在進行兩個步驟... superview superview。 (後一種方法適用於爲單元格的contentView添加子視圖的nib定義的單元格)。

這是一種更安全的方法來爬上視圖層次結構,無論細胞如何構建,它都能工作。

- (UITableViewCell *)tableViewCellContaining:(UIView *)aView { 

    UIView *answer = aView; 
    while (answer && ![answer isKindOfClass:[UITableViewCell self]]) 
     answer = answer.superview; 
    return answer; 
} 

我認爲,如果你更換這行代碼可能會工作:

UITableViewCell *cell = (UITableViewCell*)[[imgView superview]superview]; 

這一行

UITableViewCell *cell = [self tableViewCellContaining:imgView] 
相關問題