简体   繁体   English

当用户开始在UIImageView上滑动时,UITableView Swipe Action不起作用

[英]UITableView Swipe Action is not working when user begin swiping on the UIImageView

I have implemented UITableView with left swipe actions for each TableViewCell . 我已经为每个TableViewCell使用左滑动操作实现了UITableView For implementing left swipe action, I used following delegate method which is provided by the apple. 为了实现左滑动动作,我使用了由apple提供的以下委托方法。

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

}

在此输入图像描述

Above is the screen capture of my storyboard. 以上是我的故事板的屏幕截图。 Which shows the UITableViewCell UI design. 其中显示了UITableViewCell UI设计。 My swipe action is working without any problem except one occasion. 除了一次之外,我的滑动操作没有任何问题。 That is, as you can see here, on the right hand side I have included a thumbnail image ( UIImageView ). 也就是说,正如你在这里看到的那样,在右侧我已经包含了一个缩略图( UIImageView )。 Once user begin swiping by tapping on this image, the swipe action is not working. 用户通过点击此图片开始滑动后,滑动操作无效。 I have tried with disable user interactions for image view as well, but still swipe action isn't working. 我已尝试禁用用户交互以进行图像查看,但仍然无法使用滑动操作。 Anyone have an idea what's wrong with my implementation is? 任何人都知道我的实施有什么问题?

I have create UITableViewCell class and set UIOutlet to the image view. 我创建了UITableViewCell类并将UIOutlet设置为图像视图。 I'm downloading the image from url using custom method and set in to the UIImageView . 我正在使用自定义方法从url下载图像并设置为UIImageView Following code snippet will explain that. 以下代码片段将解释这一点。

@property (weak, nonatomic) IBOutlet UIImageView *imgProductIcon;

[self.imgProductIcon setImageWithURL:[NSURL URLWithString:item.itemImage] placeholderImage:[UIImage imageNamed:@"ICProduct"]];

- (void)setImageWithURL:(NSURL *)url
       placeholderImage:(UIImage *)placeholderImage
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];

    [self setImageWithURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
}

- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
              placeholderImage:(UIImage *)placeholderImage
                       success:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
                       failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure
{

    if ([urlRequest URL] == nil) {
        [self cancelImageDownloadTask];
        self.image = placeholderImage;
        return;
    }

    if ([self isActiveTaskURLEqualToURLRequest:urlRequest]){
        return;
    }

    [self cancelImageDownloadTask];

    AFImageDownloader *downloader = [[self class] sharedImageDownloader];
    id <AFImageRequestCache> imageCache = downloader.imageCache;

    //Use the image from the image cache if it exists
    UIImage *cachedImage = [imageCache imageforRequest:urlRequest withAdditionalIdentifier:nil];
    if (cachedImage) {
        if (success) {
            success(urlRequest, nil, cachedImage);
        } else {
            self.image = cachedImage;
        }
        [self clearActiveDownloadInformation];
    } else {
        if (placeholderImage) {
            self.image = placeholderImage;
        }

        __weak __typeof(self)weakSelf = self;
        NSUUID *downloadID = [NSUUID UUID];
        AFImageDownloadReceipt *receipt;
        receipt = [downloader
                   downloadImageForURLRequest:urlRequest
                   withReceiptID:downloadID
                   success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
                       __strong __typeof(weakSelf)strongSelf = weakSelf;
                       if ([strongSelf.af_activeImageDownloadReceipt.receiptID isEqual:downloadID]) {
                           if (success) {
                               success(request, response, responseObject);
                           } else if(responseObject) {
                               strongSelf.image = responseObject;
                           }
                           [strongSelf clearActiveDownloadInformation];
                       }

                   }
                   failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
                       __strong __typeof(weakSelf)strongSelf = weakSelf;
                        if ([strongSelf.af_activeImageDownloadReceipt.receiptID isEqual:downloadID]) {
                            if (failure) {
                                failure(request, response, error);
                            }
                            [strongSelf clearActiveDownloadInformation];
                        }
                   }];

        self.af_activeImageDownloadReceipt = receipt;
    }
}

If you want to implement left swipe action, I suggest you should : Use -tableView:trailingSwipeActionsConfigurationForRowAtIndexPath: instead of this method, which will be deprecated in a future release. 如果你想实现左滑动动作,我建议你应该: 使用 -tableView:trailingSwipeActionsConfigurationForRowAtIndexPath:而不是这个方法,这个方法将在以后的版本中弃用。 This method ( editActionsForRowAtIndexPath ) supersedes -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: if return value is non-nil 此方法( editActionsForRowAtIndexPath )取代-tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: if return value is non-nil

Example of use of -tableView:trailingSwipeActionsConfigurationForRowAtIndexPath: 使用-tableView:trailingSwipeActionsConfigurationForRowAtIndexPath:

if (indexPath.section == 0) {
    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"Delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        //e.g. [self.dataArray removeObjectAtIndex:indexPath.row];
        //e.g. [self.tableView reloadData];
        completionHandler (YES);
    }];
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
    return config;
} else {
    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM