2015-04-24 68 views
0

即時通訊嘗試將手勢識別器添加到所有單元格中的uiimageview中,該單元格將使該圖像上的uiimageview更改圖像,但我無法弄清楚如何告訴手勢ibaction更改該索引路徑中的圖像。添加手勢識別器來指定表格中的單元格對象

我用這段代碼實現的是,它只在最後一個單元格中工作正常,所有其他單元格也沒有得到這個手勢。

我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    // Configure the cell... 
    imgConfirm = (UIImageView *)[cell viewWithTag:107]; 
    [imgConfirm setImage:[UIImage imageNamed:@"icon2"]]; 
    [imgConfirm addGestureRecognizer:self.tapGestureM2]; 
    return cell; 
} 

- (IBAction)tapGestureTap:(UITapGestureRecognizer *)sender { 
    NSData* imgConfirmData1 = UIImagePNGRepresentation(imgConfirm.image); 
    NSData* imgConfirmData2 = UIImagePNGRepresentation([UIImage imageNamed:@"icon2"]); 

    if ([imgConfirmData1 isEqualToData:imgConfirmData2]) { 
     [imgConfirm setImage:[UIImage imageNamed:@"icon"]]; 
    } 
    else{ 
     [imgConfirm setImage:[UIImage imageNamed:@"icon2"]]; 
    } 
} 

IM對不起,如果這個問題已經被問過:)但我要尋找的時間,但無法找到合適的一個。

*編輯:在這段代碼中,每一個細胞都得到了自來水識別,但IBAction爲不解僱

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
indexpath=[self.tableViewM2 indexPathForCell:cell]; 
// Configure the cell... 
UIImageView *imgConfirm = (UIImageView *)[cell viewWithTag:107]; 
[imgConfirm setImage:[UIImage imageNamed:@"icon2"]]; 

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] init]; 
// setup gesture as needed 
[imgConfirm addGestureRecognizer:gesture]; 
return cell; 
} 

- (IBAction)tapGestureTap:(UITapGestureRecognizer *)sender { 
NSLog(@"%d,%d",indexpath.row,indexpath.section); 
UIImageView *imgConfirm = (UIImageView *)sender.view; 
NSData* imgConfirmData1 = UIImagePNGRepresentation(imgConfirm.image); 
NSData* imgConfirmData2 = UIImagePNGRepresentation([UIImage imageNamed:@"icon2"]); 

if ([imgConfirmData1 isEqualToData:imgConfirmData2]) { 
    [imgConfirm setImage:[UIImage imageNamed:@"icon"]]; 
} 
else{ 
    [imgConfirm setImage:[UIImage imageNamed:@"icon2"]]; 
} 
} 

* EDIT2:我終於找到了如何做到這一點,從下面的代碼是正確的,但需要告訴手勢動作開火!

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureTap:)]; 
+0

請注意,當用戶滾動表格視圖並重新使用單元格時,最終會爲每個圖像視圖添加多個手勢識別器。 – rmaddy

回答

1

您應該從手勢中獲取圖像視圖。 imgConfirm不需要使用實例變量。您還需要爲每個圖像視圖創建一個單獨的手勢識別器。你不能重複使用同一個。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    // Configure the cell... 
    UIImageView *imgConfirm = (UIImageView *)[cell viewWithTag:107]; 
    [imgConfirm setImage:[UIImage imageNamed:@"icon2"]]; 
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] inittWithTarget:self action:@selector(tapGestureTap:)]; 
    // setup gesture as needed 
    [imgConfirm addGestureRecognizer:gesture]; 
    return cell; 
} 

- (IBAction)tapGestureTap:(UITapGestureRecognizer *)sender { 
    UIImageView *imgConfirm = (UIImageView *)sender.view; 
    NSData* imgConfirmData1 = UIImagePNGRepresentation(imgConfirm.image); 
    NSData* imgConfirmData2 = UIImagePNGRepresentation([UIImage imageNamed:@"icon2"]); 

    if ([imgConfirmData1 isEqualToData:imgConfirmData2]) { 
     [imgConfirm setImage:[UIImage imageNamed:@"icon"]]; 
    } 
    else{ 
     [imgConfirm setImage:[UIImage imageNamed:@"icon2"]]; 
    } 
} 

這裏缺少的一件重要的事情是,如果用戶滾動表格,圖像將被重置。您需要添加更多代碼來跟蹤每個圖像的當前狀態,以便您的cellForRowAtIndexPath:設置正確的圖像。

+0

更改圖像後需要'reloadTableData'或'tableviewcell'。 –

+1

@ViralSavaj不,你不知道。 – rmaddy

+0

感謝您的幫助,但它似乎仍然不適合我,它只爲最後一個單元格工作... setimage適用於所有單元格,但addGestureRecognizer只能在最後一個,方法還有:( – Tj3n

相關問題