簡體   English   中英

添加手勢識別器以指定表格中的單元格對象

[英]Add Gesture Recognizer to specify object of cell in table

我試圖在所有單元格的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"]];
    }
}

我很抱歉,如果以前已經問過這個問題:)但我搜索了一個小時,但找不到合適的一個。

*編輯:在此代碼中,每個單元格都獲得了敲擊識別器,但未觸發

- (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:)];

您應該從手勢中獲取圖像視圖。 無需為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:設置正確的圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM