首页 > 解决方案 > 当用户开始在 UIImageView 上滑动时,UITableView 滑动操作不起作用

问题描述

我已经UITableView为每个TableViewCell. 为了实现向左滑动动作,我使用了苹果提供的以下委托方法。

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

}

在此处输入图像描述

以上是我的故事板的屏幕截图。其中显示了UITableViewCellUI 设计。除了一次之外,我的滑动操作没有任何问题。也就是说,正如您在此处看到的,在右侧我包含了一个缩略图 ( UIImageView)。一旦用户通过点击此图像开始滑动,滑动操作将不起作用。我也尝试过禁用图像视图的用户交互,但滑动操作仍然不起作用。有人知道我的实现有什么问题吗?

我已经创建UITableViewCell类并将 UIOutlet 设置为图像视图。我正在使用自定义方法从 url 下载图像并设置为UIImageView. 以下代码片段将对此进行解释。

@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;
    }
}

标签: iosobjective-cuitableviewuiswipeactionsconfiguration

解决方案


如果你想实现向左滑动动作,我建议你应该:使用 -tableView:trailingSwipeActionsConfigurationForRowAtIndexPath:这个方法来代替,这个方法将在未来的版本中被弃用。如果返回值非零,则此方法 ( editActionsForRowAtIndexPath) 取代-tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:

使用示例-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 {
    //...
}

推荐阅读