即時通訊嘗試將手勢識別器添加到所有單元格中的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:)];
請注意,當用戶滾動表格視圖並重新使用單元格時,最終會爲每個圖像視圖添加多個手勢識別器。 – rmaddy