【问题标题】:UITableView Swipe Action is not working when user begin swiping on the UIImageView当用户开始在 UIImageView 上滑动时,UITableView 滑动操作不起作用
【发布时间】:2019-07-09 08:01:47
【问题描述】:

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

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

}

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

我创建了UITableViewCell 类并将 UIOutlet 设置为图像视图。我正在使用自定义方法从 url 下载图像并设置为UIImageView。下面的代码 sn -p 将解释这一点。

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

【问题讨论】:

  • 请出示您的代码
  • 你能显示你的代码吗?我想也许你将缩略图添加到父视图中。请再次检查您的对象。
  • 我添加了一些代码段。希望这些就足够了

标签: ios objective-c uitableview uiswipeactionsconfiguration


【解决方案1】:

如果你想实现左滑动作,我建议你应该:使用 -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 {
    //...
}

【讨论】:

  • 我的应用程序使用 iOS 9.0 作为最低部署目标。我在使用此方法时遇到问题,因为它仅支持 iOS 11 及更高版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多