// // ViewController.swift // iOSTableViewImageFileManager // // Created by USER1 on 10.12.16. // Copyright © 2016 HSHARZ. All rights reserved. // import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { var directories = [DirItem]() // Outlets @IBOutlet var tableview: UITableView! @IBOutlet var bnCdParent: UIButton! @IBOutlet var bnCdItem: UIButton! @IBOutlet var bnShowPictures: UIButton! var actualPath:String="" let fileManager : FileManager = FileManager() var homedir = "" func getHomeDir(_ path:String) -> String { var newPath="" var anz=0 for ch in path.characters { if ch == "/" { anz += 1 if anz == 3 { break } } newPath.append(ch) } return newPath } func getParent(_ path:String) -> String { // bestimme Anzahl der / var anzComplete=0 for ch in path.characters { if ch == "/" { anzComplete += 1 } } var newPath="" var anz = 0 for ch in path.characters { if ch == "/" { anz += 1 if anz == anzComplete { break } } newPath.append(ch) } return newPath } //getParent override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. homedir = NSHomeDirectory() //homedir = "/Users/user1" //let s = "/Users/user1/test" homedir = getHomeDir(homedir) var path = FileManager.default.currentDirectoryPath // ist root // let path = FileManager.default.createFile(atPath: <#T##String#>, contents: <#T##Data?#>, attributes: <#T##[String : Any]?#>) // let path = FileManager.default.fileExists(atPath: <#T##String#>) path = NSString(string: "~/Desktop").expandingTildeInPath getDirItems(homedir) tableview.delegate=self tableview.dataSource=self } @IBAction func SwipeActionClick(_ sender: UISwipeGestureRecognizer) { getDirItems( getParent(actualPath) ) } @IBAction func bnCdItemClick(_ sender: UIButton) { if let indexPath = tableview.indexPathForSelectedRow { let dir:DirItem = directories[(indexPath as NSIndexPath).row] if dir.dirname == ".." { if actualPath != homedir { getDirItems( getParent(actualPath) ) } } else { getDirItems(dir.path+"/"+dir.dirname) } } } // bnCdItemClick func getDirItems(_ path:String) { actualPath=path directories.removeAll() // NSTemporaryDirectory do { let filesInDirectory = try fileManager.contentsOfDirectory(atPath: path) as? [String] if let files = filesInDirectory { if files.count > 0 { if actualPath != homedir { directories.append( DirItem("..", actualPath) ) } for item in files { if !item.hasPrefix(".") { directories.append( DirItem(item, path) ) } } } else { // Meldung } } // if let files = filesInDirectory { } catch let error { } tableview.reloadData() bnCdItem.isEnabled=false } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return directories.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "mycell") cell.textLabel!.text = "xx" let dir:DirItem = directories[(indexPath as NSIndexPath).row] // as! Stadt cell.textLabel!.text = dir.dirname cell.detailTextLabel!.text=dir.path return cell } func tableView(_: UITableView, didSelectRowAt: IndexPath) { bnCdItem.isEnabled=true // let row = (didSelectRowAt as NSIndexPath).row // labelStadt.text="row: "+String(row) } // wird aufgerufen nach dem Schalter Click override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let button:UIButton = sender as? UIButton { if button==bnShowPictures { if let dest = segue.destination as? ShowPicturesViewController { dest.pictPath = actualPath } } } // if button } // prepare func prepare2(for segue: UIStoryboardSegue, sender: Any?) { if let button:UIButton = sender as? UIButton { if button==bnShowPictures { if let indexPath = tableview.indexPathForSelectedRow { let dir:DirItem = directories[(indexPath as NSIndexPath).row] if let dest = segue.destination as? ShowPicturesViewController { dest.pictPath = actualPath } } } } // if button } // prepare // Rücksprung vom ShowPictureViewController Abbruch @IBAction func unwindToMainViewController(_ segue:UIStoryboardSegue) { } // unwindToEditViewCancel override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }