ドキュメント下のCSVファイルをテーブルに表示させたいと考えています。
①試したこと
ドキュメント下のファイル名を取得(表示させるのはファイル名だけで良い)
TableViewをOutlet接続し、cellにファイル名を返すようにした
②上手くいかなかったこと
エラーは出ていないが、テーブルにファイル名が表示されない
(ファイル名を取得できていない?取得方法が間違えている?テーブルの設定がなされていない?)
ドキュメントのパスも取得しているので表示されると思うのですが、、、
わかる方いらっしゃいましたら、よろしくお願いします。
③該当コード
swift
1import UIKit 2 3class sampleViewController: UIViewController,UITableViewDataSource { 4 5let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] 6 7 8 var fileNames: [String] { 9 do { 10 return try FileManager.default.contentsOfDirectory(atPath: documentPath) 11 } catch { 12 return [] 13 } 14 } 15 16 @IBOutlet weak var CsvTableiew: UITableView! 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 20 // Do any additional setup after loading the view. 21 } 22 23 override func didReceiveMemoryWarning() { 24 super.didReceiveMemoryWarning() 25 // Dispose of any resources that can be recreated. 26 } 27 28 func tableView(_ tableView:UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell { 29 let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] 30 31 32 var fileNames: [String] { 33 do { 34 return try FileManager.default.contentsOfDirectory(atPath: documentPath) 35 } catch { 36 return [] 37 } 38 } 39 //セルを取得する。 40 let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for:indexPath as IndexPath) as UITableViewCell 41 42 //カンマでデータを分割して配列に格納する。 43 let dataDetail = fileNames[indexPath.row] 44 cell.textLabel?.text = dataDetail 45 46 return cell 47 } 48 func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int { 49 return fileNames.count 50 } 51}
④開発環境
xcode8 swift3 MacBook Pro
よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/11 13:12
退会済みユーザー
2018/01/11 13:27
退会済みユーザー
2018/01/11 14:08
2018/01/12 03:48
2018/01/12 04:47
退会済みユーザー
2018/01/12 07:59
2018/01/12 15:37