我正在尝试在iOS13的表格视图中实现“上下文菜单”(也尝试了集合视图,它们有相同的问题):
我按照此处的说明进行操作,创建了包含一堆照片的收藏夹视图,当进行3d触摸时,我将获得带有动作列表的预览,如下所示:
UITableView中的委托方法:
- (void)tableView:(UITableView *)tableView willCommitMenuWithAnimator:
(id<UIContextMenuInteractionCommitAnimating>)animator;
Run Code Online (Sandbox Code Playgroud)
并在collectionView中
- (void)collectionView:(UICollectionView *)collectionView willCommitMenuWithAnimator:
(id<UIContextMenuInteractionCommitAnimating>)animator;
Run Code Online (Sandbox Code Playgroud)
两者都没有“ indexPath”;
因此,我无法传递正在查看的当前单元格或索引。
我要实现此委托方法,因此当我点击预览图像时,它将导航到包含绑定到当前单元格的信息的预期页面。
我想要和尝试的是这样的:
func collectionView(_ collectionView: UICollectionView, willCommitMenuWithAnimator animator: UIContextMenuInteractionCommitAnimating) {
animator.addAnimations {
self.show(PreviewViewController(image: UIImage(named: "indexpathString")), sender: self)
}
}
Run Code Online (Sandbox Code Playgroud)
并且indexpathString与当前选定的单元格或集合有关,因此我可以基于它初始化viewController。
请让我知道是否还有其他方法可以做到这一点。
谁能帮助我理解我创建的这段代码:
let cq = DispatchQueue(label: "downloadQueue", attributes: .concurrent)
cq.sync {
for i in 0..<10 {
sleep(2)
print(i)
}
}
print("all finished!")
Run Code Online (Sandbox Code Playgroud)
输出是串行顺序 1->10,中间等待 2 秒。最后会打印出all finished
我明白最后一部分。然而我的问题是:
并发队列不应该同时启动多个任务吗?所以我最初的想法是:应该按1-10打印concurrently,不一定按序列顺序。
谁能解释一下sync并发队列调用的目的,并给我一个例子,为什么以及何时需要它?