| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

ナビゲーションに移動 検索に移動
2,448 バイト追加 、 2021年11月20日 (土) 08:56
----
*https://developer.apple.com/documentation/swiftui/table
 
*Service
<pre>
import Foundation
import AppKit
import SwiftUI
 
class File : Identifiable {
let id = UUID()
let path: String
init(path: String) {
self.path = path
}
}
 
class FileList : ObservableObject {
@Published var files: [File] = []
}
 
public struct EncConverterService {
static func chooseDir() -> String {
let dialog = NSOpenPanel();
 
dialog.title = "Choose a file| Our Code World"
dialog.showsResizeIndicator = true
dialog.showsHiddenFiles = false
dialog.allowsMultipleSelection = false
dialog.canChooseDirectories = true
dialog.canChooseFiles = false
 
if (dialog.runModal() == NSApplication.ModalResponse.OK) {
let result = dialog.url
 
if (result != nil) {
let path: String = result!.path
return path
}
}
return "";
}
static func loadFile(directoryPath: String, filepaths: FileList) {
print(directoryPath)
filepaths.files.append(File(path: directoryPath))
do {
let fm = FileManager.default
let fileNames = try fm.contentsOfDirectory(atPath: directoryPath)
for fileName in fileNames {
let childPath = directoryPath + "/" + fileName
var isDir = ObjCBool(false)
fm.fileExists(atPath: childPath, isDirectory: &isDir)
if isDir.boolValue {
loadFile(directoryPath: childPath, filepaths: filepaths)
}
print("\t" + childPath)
filepaths.files.append(File(path: childPath))
}
} catch {
print(error)
}
}
}
</pre>
*View
<pre>
import SwiftUI
 
struct ContentView: View {
@ObservedObject var filePaths = FileList()
@State private var selectedPaths = Set<File.ID>()
var body: some View {
Button(action: {
let rootDir = EncConverterService.chooseDir()
EncConverterService.loadFile(directoryPath: rootDir, filepaths: self.filePaths)
}) {
Text("Choose dir")
}
Table(filePaths.files, selection: $selectedPaths) {
TableColumn("Path", value: \.path)
}
Text("\(selectedPaths.count) path selected")
 
}
}
</pre>
==コードサンプル(ロジック)==

案内メニュー