[英]Need click event of the image which is to be displayed in table view in iphone sdk
我需要通過單元格的addSubview方法添加的圖像上的click事件,並在表格視圖中設置收藏夾和收藏夾標志。 請注意,我不需要單元格的didSelectRowAtIndexPath方法來完成此任務。 請參考以下代碼:
- (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上獲得點擊事件?
我建議您創建UITableViewCell的子類。 將其作為屬性提供給UIImageView。 然后在該子類中,向UIIMageView添加一個UITapgestureRecognizer,並讓它在自身上調用一些選擇器(例如-(void)imageViewTapped:(id)發送者)。 給子類一個屬性“代理”,並使其采用UITableViewCell的.h文件中定義的協議。 添加類似的方法
- (void) imageViewTappedOnTableViewCell: (YourSubclassOfUITableViewCell *) cell;
然后可以在添加到拍子識別器的選擇器(例如imageViewTapped :)中的委托上調用此方法。 返回viewControllers cellForRowAtIndexPath:您可以將單元格的委托設置為該viewController。 然后在viewController上實現協議中的方法。 向您發送被點擊為參數的單元格時,您可以向tableView請求該單元格的indexPath並采取相應的措施。
非常清晰的設計以及您正在尋找的東西(請注意didSelectCellForRowAtIndexPath:幾乎在做同樣的概念性t(鉸鏈,除了它為您提供了indexPath而不是單元格)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.