2013-11-02 36 views
0

我需要單元格的addSubview方法添加的圖像上的點擊事件,並在表格視圖中設置最喜歡和不喜歡的標誌。請注意,我不需要cell的didSelectRowAtIndexPath方法來實現這個任務。請參閱下面的代碼:需要在iphone的表格視圖中顯示圖像的點擊事件sdk

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 

    UIImage *myImage = [[UIImage alloc]init]; 
    myImage = [UIImage imageNamed: @"Favourite.png"]; 
    UIImageView *imgName = [[UIImageView alloc]initWithImage:myImage]; 
    imgName.frame = CGRectMake(270, 10, 25, 25); 
    [cell addSubview:imgName]; 


    cell.textLabel.text = @"Hello"; 

    return cell; 
} 

我如何得到imgName上的點擊事件?

+1

你可以聲明A按鈕並將圖片設置爲按鈕,然後使用ibaction創建點擊。 –

+0

我想我們正在談論點擊。點擊在UIKit中不存在。 – Joride

+0

感謝XCode Monkey:您的解決方案適用於我:D –

回答

0

我會建議你創建一個UITableViewCell的子類。將其作爲UIImageView的屬性提供。然後在該子類中,將UITapgestureRecognizer添加到UIIMageView,並讓它調用self上的某個選擇器(例如 - (void)imageViewTapped:(id)sender)。 爲該子類提供一個屬性「委託」,並使其適用於在UITableViewCell的.h文件中定義的協議。添加方法類似

- (void) imageViewTappedOnTableViewCell: (YourSubclassOfUITableViewCell *) cell; 

該方法然後可以在您添加到水龍頭識別器的選擇(例如imageViewTapped :)委託調用。 回到viewControllers cellForRowAtIndexPath:你可以將單元的委託設置爲那個viewController。然後在viewController上實現該協議的方法。當您發送作爲參數點擊的單元格時,您可以向tableView索取該單元格的indexPath並採取相應的行動。

非常明確的設計,並且你正在尋找(注意didSelectCellForRowAtIndexPath正是:幾乎做同樣的概念T(興,但它給你的indexPath而不是細胞)

相關問題