ios - 当用户开始在 UIImageView 上滑动时,UITableView 滑动操作不起作用
问题描述
我已经UITableView
为每个TableViewCell
. 为了实现向左滑动动作,我使用了苹果提供的以下委托方法。
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
}
以上是我的故事板的屏幕截图。其中显示了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;
}
}
解决方案
如果你想实现向左滑动动作,我建议你应该:使用 -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 {
//...
}
推荐阅读
- quantum-computing - 让 Grammarinator 使用 openqasm3 语法
- ios - 如何将值表单视图传递给 SwiftUI 中的视图模型?
- python - 与 pyppeteer 一起使用时,Pytest-asyncio 不会移动到下一条语句
- php - 如何在 laravel API 中实现 websockets
- javascript - 有没有办法列出所有 nodejs 函数执行时间?
- javascript - 如何将字符串转换为 JSON 对象并在反应组件中显示?
- flutter - 在 Flutter Web 中使用不包含 Navigator 的上下文请求的 Navigator 操作
- flutter - 如何在flutter中返回屏幕后刷新数据
- android-studio - 更改 Android Studio 的位置
- php - 亚马逊产品广告 API - PHP 致命错误