Guest User

Untitled

a guest
Oct 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. var steppers : UIStepper?
  2. @IBOutlet weak var hourLabel: UILabel!
  3.  
  4. @IBAction func stepper(_ sender: UIStepper) {
  5.  
  6. hourLabel.text = String(sender.value)
  7. }
  8.  
  9. import UIKit
  10. import AVFoundation
  11.  
  12. class SelectClass: UIViewController, UITableViewDelegate, UITableViewDataSource {
  13.  
  14.  
  15. @IBOutlet weak var tableView: UITableView!
  16.  
  17. var list : [QCategoryy] = [QCategoryy]()
  18. var audioPlayer : AVAudioPlayer!
  19. var limit = 3
  20.  
  21.  
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24.  
  25. tableView.delegate = self
  26. tableView.dataSource = self
  27. tableView.allowsMultipleSelection = true
  28. self.title = "Categories"
  29. list = NearbyPlaces.getCategories()
  30.  
  31.  
  32.  
  33.  
  34. }
  35.  
  36.  
  37.  
  38. override func viewWillAppear(_ animated: Bool) {
  39. super.viewWillAppear(animated)
  40.  
  41.  
  42. list.sort() { $0.views > $1.views}
  43. tableView.reloadData()
  44. }
  45.  
  46.  
  47. override func didReceiveMemoryWarning() {
  48. super.didReceiveMemoryWarning()
  49. // Dispose of any resources that can be recreated.
  50. }
  51.  
  52.  
  53.  
  54. @IBAction func backTapp(_ sender: Any) {
  55.  
  56. let audioUrl = NSURL.fileURL(withPath: Bundle.main.path(forResource: "pop_drip", ofType: "m4a")!)
  57. do{
  58. try AVAudioSession.sharedInstance().setActive(true)
  59. try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
  60.  
  61. try audioPlayer = AVAudioPlayer(contentsOf: audioUrl)
  62. audioPlayer.prepareToPlay()
  63. audioPlayer.play()
  64. }
  65. catch _ as NSError
  66. {
  67.  
  68. }
  69. dismiss(animated: true, completion: nil)
  70.  
  71. }
  72.  
  73.  
  74. @IBAction func doneTapp(_ sender: Any) {
  75.  
  76. let audioUrl = NSURL.fileURL(withPath: Bundle.main.path(forResource: "pop_drip", ofType: "m4a")!)
  77. do{
  78. try AVAudioSession.sharedInstance().setActive(true)
  79. try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
  80.  
  81. try audioPlayer = AVAudioPlayer(contentsOf: audioUrl)
  82. audioPlayer.prepareToPlay()
  83. audioPlayer.play()
  84. }
  85. catch _ as NSError
  86. {
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93. self.performSegue(withIdentifier: nearbySearchSegueIdentifier, sender: nil)
  94. }
  95.  
  96.  
  97.  
  98.  
  99. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  100. return list.count
  101. }
  102.  
  103. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  104. let identifier = "CATEGORY_CELL"
  105. let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
  106. let selectedIndexPaths = tableView.indexPathsForSelectedRows
  107. let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
  108. /* cell.accessoryType = rowIsSelected ? .checkmark : .none */
  109. cell.accessoryType = list[indexPath.row].isSelected ? .checkmark : .none
  110. cell.textLabel?.text = list[indexPath.row].name
  111. return cell
  112. }
  113.  
  114. let nearbySearchSegueIdentifier = "goToMcourse"
  115.  
  116. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  117.  
  118. let cell = tableView.cellForRow(at: indexPath)!
  119. cell.accessoryType = .checkmark
  120.  
  121.  
  122. /* self.performSegue(withIdentifier: nearbySearchSegueIdentifier, sender: list[indexPath.row]) */
  123. }
  124.  
  125. func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
  126.  
  127. let cell = tableView.cellForRow(at: indexPath)!
  128. cell.accessoryType = .none
  129.  
  130. }
  131.  
  132.  
  133. func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
  134. if let sr = tableView.indexPathsForSelectedRows {
  135. if sr.count == limit {
  136. let alertController = UIAlertController(title: "Oops", message:
  137. "You are limited to (limit) selections", preferredStyle: .alert)
  138. alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {action in
  139. }))
  140. self.present(alertController, animated: true, completion: nil)
  141.  
  142. return nil
  143. }
  144. }
  145.  
  146. return indexPath
  147.  
  148.  
  149. }
  150.  
  151.  
  152.  
  153.  
  154.  
  155. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  156.  
  157. if segue.identifier == nearbySearchSegueIdentifier {
  158. guard let category = sender as? QCategoryy else {
  159. return
  160. }
  161. if let selectedRows = tableView.indexPathsForSelectedRows {
  162.  
  163. if let vc = segue.destination as? CourseClass2 {
  164. vc.category = category
  165. }
  166. }
  167. }
  168. }
  169.  
  170.  
  171. }
  172.  
  173.  
  174.  
  175.  
  176. extension QCategoryy {
  177. private static let ketPrefix = "category-"
  178.  
  179. var views:Int {
  180. get {
  181. return UserDefaults.standard.integer(forKey: QCategoryy.ketPrefix + name)
  182. }
  183. }
  184.  
  185. func markView() {
  186. UserDefaults.standard.set(views + 1, forKey: QCategoryy.ketPrefix + name)
  187. }
  188. }
  189.  
  190. var limit = 3
  191.  
  192. func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
  193. if let sr = tableView.indexPathsForSelectedRows {
  194. if sr.count == limit {
  195. let alertController = UIAlertController(title: "Oops", message:
  196. "You are limited to (limit) selections", preferredStyle: .alert)
  197. alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {action in
  198. }))
  199. self.present(alertController, animated: true, completion: nil)
  200.  
  201. return nil
  202. }
  203. }
  204.  
  205. return indexPath
  206.  
  207.  
  208. }
Add Comment
Please, Sign In to add comment