This repository was archived by the owner on Jun 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableViewController.swift
More file actions
108 lines (80 loc) · 3.45 KB
/
TableViewController.swift
File metadata and controls
108 lines (80 loc) · 3.45 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
106
107
108
import UIKit
import Cartography
import AVFoundation
import CoreMotion
class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView?
var audioFileList = NSMutableArray()
let directoryURL = NSURL(string: "/System/Library/Audio/UISounds")!
var currentSoundSelected: NSIndexPath?
let sound = Sound()
weak var app = UIApplication.sharedApplication().delegate as? AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
self.tableView = UITableView(frame: CGRectZero, style: .Grouped)
self.tableView?.translatesAutoresizingMaskIntoConstraints = false
self.tableView?.delegate = self
self.tableView?.dataSource = self
self.view.backgroundColor = UIColor.whiteColor()
let fileManager = NSFileManager.defaultManager()
guard let enumerator = fileManager.enumeratorAtPath("/System/Library/Audio/UISounds") else {
app?.cannotFindSystemSoundErrorAlert()
return
}
while let element = enumerator.nextObject() as? String {
if ((sound.trackList[element]?.hasSuffix("caf")) != nil) {
audioFileList.addObject(element)
}
}
}
override func viewWillLayoutSubviews() {
if let tv = self.tableView {
view.addSubview(tv)
constrain(view, tv) { view, tableView in
tableView.edges == view.edges
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sound.trackList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
//Give the cells Labels according to the newly combined trackList
cell.textLabel?.text = sound.trackList[self.audioFileList[indexPath.row] as! String]
if self.currentSoundSelected == indexPath.row {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
else {
cell.accessoryType = UITableViewCellAccessoryType.None;
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let
currentSoundSelected = self.currentSoundSelected,
uncheckCell = tableView.cellForRowAtIndexPath(currentSoundSelected)
{
uncheckCell.accessoryType = .None
}
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
self.currentSoundSelected = indexPath
if let rowSelected = sound.trackList[self.audioFileList[indexPath.row] as! String] {
self.navigationItem.title = "Selected Sound: \(rowSelected)"
}
self.playSound(indexPath.row)
}
func playSound(whatToPlay: Int) {
let fileName: NSURL = directoryURL.URLByAppendingPathComponent(
audioFileList.objectAtIndex(whatToPlay) as! String
)
if !NSFileManager.defaultManager().fileExistsAtPath("\(fileName)") {
return
}
sound.playSpecifiedURL(fileName)
var soundID: SystemSoundID = 0
AudioServicesCreateSystemSoundID(fileName, &soundID)
AudioServicesPlaySystemSound(soundID)
}
}