FileListTableView.swift
3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// FlieListTableView.swift
// FileBrowser
//
// Created by Roy Marmelstein on 14/02/2016.
// Copyright © 2016 Roy Marmelstein. All rights reserved.
//
import Foundation
import UIKit
extension FileListViewController: UITableViewDataSource, UITableViewDelegate {
//MARK: UITableViewDataSource, UITableViewDelegate
func numberOfSections(in tableView: UITableView) -> Int {
if searchController.isActive {
return 1
}
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive {
return filteredFiles.count
}
return sections[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "FileCell"
var cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier)
if let reuseCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) {
cell = reuseCell
}
cell.selectionStyle = .blue
let selectedFile = fileForIndexPath(indexPath)
cell.textLabel?.text = selectedFile.displayName
cell.imageView?.image = selectedFile.type.image()
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedFile = fileForIndexPath(indexPath)
searchController.isActive = false
if selectedFile.isDirectory {
let fileListViewController = FileListViewController(initialPath: selectedFile.filePath)
fileListViewController.didSelectFile = didSelectFile
self.navigationController?.pushViewController(fileListViewController, animated: true)
}
else {
if let didSelectFile = didSelectFile {
self.dismiss()
didSelectFile(selectedFile)
}
else {
let filePreview = previewManager.previewViewControllerForFile(selectedFile, fromNavigation: true)
self.navigationController?.pushViewController(filePreview, animated: true)
}
}
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if searchController.isActive {
return nil
}
if sections[section].count > 0 {
return collation.sectionTitles[section]
}
else {
return nil
}
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
if searchController.isActive {
return nil
}
return collation.sectionIndexTitles
}
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
if searchController.isActive {
return 0
}
return collation.section(forSectionIndexTitle: index)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCell.EditingStyle.delete) {
let selectedFile = fileForIndexPath(indexPath)
selectedFile.delete()
prepareData()
tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.automatic)
}
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return allowEditing
}
}