2012-04-05 31 views
0

我有以下代碼不起作用:我永遠不會去goToFoodDetail在uitableviewcell中捕捉uiimageviewe touch

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"<#MyCell#>"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 
    } 

    NSInteger objectLocation = indexPath.row; 

    UILabel* lblText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 260, 20)]; 
    [lblText setFont:[UIFont fontWithName:@"Helvetica" size:10.0]]; 
    [lblText setText:[food foodName]]; 
    [cell addSubview:lblText]; 

    UILabel* lblType = [[UILabel alloc] initWithFrame:CGRectMake(0, 21, 260, 20)]; 
    [lblType setFont:[UIFont fontWithName:@"Helvetica" size:9.0]]; 
    lblType.textColor = [UIColor blueColor ]; 
    [lblType setText:[food foodType]]; 
    [cell addSubview:lblType]; 

    UIImageView * detailImage = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"paris.jpg"]] autorelease]; 
    detailImage.frame = CGRectMake(270, 4, 40, 36); 
    cell.imageView.userInteractionEnabled = YES; 
    cell.imageView.tag = indexPath.row; 

    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToFoodDetail:)]; 
    tapped.numberOfTapsRequired = 1; 
    [cell.imageView addGestureRecognizer:tapped]; 
    [tapped release]; 

    [cell addSubview:detailImage]; 
    [detailImage release]; 
    [lblText release]; 
    [lblType release]; 
    return cell; 
} 

-(void)goToFoodDetail :(id) sender 
{ 
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender; 
    NSLog(@"Tag = %d", gesture.view.tag); 
} 

回答

0

它看起來像你創建一個新的UIImageView顯示圖像Paris.jp並補充說,你的細胞,但後來加入了點觸手勢識別到該單元的默認圖像視圖,而不是設置它的形象。你打算在每個單元中有兩個圖像視圖,還是隻有一個?

如果您打算讓單元格的圖像視圖都顯示圖像並且可以點擊,那麼您不需要創建其他圖像視圖。你可以設置cell.imageView.image = [UIImage imageWithName:「Paris.jog」]並附加手勢識別器。

+0

只有一個。謝謝,我會試試這個,讓你知道。 – Nir 2012-04-05 18:23:29