Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios SwiftUI列表视图打开相同的PDF文件,无论选择哪一行-也不能删除列表的扩展名_Ios_Swift_Swiftui - Fatal编程技术网

Ios SwiftUI列表视图打开相同的PDF文件,无论选择哪一行-也不能删除列表的扩展名

Ios SwiftUI列表视图打开相同的PDF文件,无论选择哪一行-也不能删除列表的扩展名,ios,swift,swiftui,Ios,Swift,Swiftui,我列出了一个包中存储的PDF数组。然而,无论我选择哪一行,它总是显示列表中的第一项。我不明白为什么会发生这种情况,因为我在提交工作表时使用的是pdf[item] 我遇到的另一个问题是,我似乎无法删除所列文档的扩展名。我曾尝试将dropLast(4)添加到let=pdf中,但似乎没有任何效果 注意:(bundleLoc根据其他选择而变化,但为了解释,我已将其更改为“/Products/PDF” 这是我列出所有PDF的地方: struct ProductTab5View: View {

我列出了一个包中存储的PDF数组。然而,无论我选择哪一行,它总是显示列表中的第一项。我不明白为什么会发生这种情况,因为我在提交工作表时使用的是
pdf[item]

我遇到的另一个问题是,我似乎无法删除所列文档的扩展名。我曾尝试将
dropLast(4)
添加到
let=pdf
中,但似乎没有任何效果

注意:(
bundleLoc
根据其他选择而变化,但为了解释,我已将其更改为
“/Products/PDF”

这是我列出所有PDF的地方:

struct ProductTab5View: View {
    
    @State var showingDetail = false
    
    var body: some View {
        ScrollView(.vertical){
            VStack(alignment: .leading){
                let bundleLoc = "/Products/PDF"
                let pdf = Bundle.main.urls(forResourcesWithExtension: "pdf", subdirectory: bundleLoc)?
                    .compactMap { $0.lastPathComponent } ?? []
                    .dropLast(4)

                ForEach(0..<pdf.count, id: \.self) { item in
                    Button(action: {
                        self.showingDetail.toggle()
                    }, label: {
                        Text(pdf[item])
                    })
                    .sheet(isPresented: $showingDetail) {
                        PDFKitView(pdfName: pdf[item], pdfLocation: bundleLoc)
                    }
                }
            }
        }
    }
}

这里有一些问题

首先,要删除
.pdf
扩展名,您需要在
字符串
上使用
dropLast(4)
,而不是由
compactMap
生成的
数组

其次,实际上您正在为
ForEach
循环的每个迭代创建一个
.sheet
。所有的工作表都由
$showingDetail
控制其可见性,但您只能看到最上面(第一个)的工作表

我想提出一些改进建议:

  • 存储
    URL
    数组,而不是
    String
    ——这样您就不需要在
    PDFKitView
    中重新创建
    URL
  • 使用
    @State
    var指示应显示哪个
    URL
  • 直接迭代数组。无需使用
    索引

  • 感谢您的深入回答!为了测试这一点,我需要在
    struct PDFKitView:View
    中的预览中添加一个
    @Binding URL
    。我需要在其中添加什么作为
    @Binding URL
    示例?您可以使用指向PDF
    常量的任何URL(URL(字符串):https://somewebsite.com/somefile.pdf")!)
    太棒了!非常感谢,这真是太棒了。不必再写一个问题或编辑这个问题,是否可以对bundle进行排序
    let=pdf
    ?我曾尝试向其添加
    sorted()
    ,但我遇到了
    引用实例方法'sorted()的错误“on”Sequence“要求“URL”符合“Comparable
    您不能只使用
    sorted
    ,您必须使用
    sorted
    作为尾随闭包,以执行您想要的任何比较;可能是按文件名排序
    struct PDFKitView: View {
        
        var pdfName: String
        var pdfLocation: String
        
        var body: some View {
            PDFViewer(url: Bundle.main.url(forResource: self.pdfName, withExtension: nil, subdirectory: pdfLocation)!)
                .ignoresSafeArea(edges:.bottom)
        }
    }
    
    struct ProductTab5View: View {
        
        @State var showingDetail = false
        @State var pdfURL: URL?
        
        var body: some View {
            ScrollView(.vertical){
                VStack(alignment: .leading){
                    let bundleLoc = "/Products/PDF"
                    let pdfs = Bundle.main.urls(forResourcesWithExtension: "pdf", subdirectory: bundleLoc) ?? []
                    ForEach(pdfs, id: \.self) { pdf in
                        Button(action: {
                            self.pdfURL = pdf
                            self.showingDetail.toggle()
                        }, label: {
                            Text((pdf.lastPathComponent ?? "").dropLast(4))
                        })
                    }
                    .sheet(isPresented: $showingDetail) {
                            PDFKitView(pdf: self.$pdfURL)
                    }
                }
            }
        }
    }
        
    struct PDFKitView: View {
        @Binding var pdf: URL?
        var body: some View {
            if let url = self.pdf { 
                PDFViewer(url: url)
                    .ignoresSafeArea(edges:.bottom)
            } else {
                Text("PDF Not found")
            }
            
        }
    }