用户开始在UIImageView上滑动时,UITableView滑动操作不起作用

时间:2019-07-09 08:01:47

标签: ios objective-c uitableview uiswipeactionsconfiguration

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

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

}

enter image description here

上面是我的故事板的屏幕截图。该图显示了UITableViewCell UI设计。除一次外,我的滑动动作没有任何问题。也就是说,如您在此处看到的,我在右侧包括了缩略图(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;
    }
}

1 个答案:

答案 0 :(得分:0)

如果要实施向左滑动操作,建议您:使用 -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 {
    //...
}