Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. //
  2. // NewsTableViewController.swift
  3. // ShkabaiProject
  4. //
  5. // Created by Admin on 23/08/2018.
  6. // Copyright © 2018 Admin. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Alamofire
  11. import PINRemoteImage
  12.  
  13. private let reuseHeadCellNews = "headNewsCell"
  14. private let reuseIndicCellNews = "indicLoadNewsCell"
  15.  
  16. class NewsTableViewController: UITableViewController {
  17.  
  18. var loadDataShkabai = LoadDataShkabai()
  19. var arrayNameCategoryNews: [String] = []
  20. var arrayNewsCategory: [[tjeratNews]] = []
  21. var opensection: [String: Bool] = [:]
  22. var height = 0
  23. var tapCell: IndexPath = IndexPath(row: 10, section: 10)
  24. var itemHeights: [String: CGFloat] = [:]
  25. var test = NewsTableViewCell()
  26.  
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. tableView.register(UINib(nibName: "NewsTableViewCell", bundle: nil), forCellReuseIdentifier: reuseHeadCellNews)
  30. // tableView.register(UINib(nibName: "NewsUpdateTableViewCell", bundle: nil), forCellReuseIdentifier: reuseIndicCellNews)
  31. //tableView.delegate = NewsTableViewCell()
  32. tableView.estimatedRowHeight = 160
  33. NotificationCenter.default.addObserver(self, selector: #selector(reloadTableVC), name: NSNotification.Name(rawValue: "reloadTableVC"), object: nil)
  34. NotificationCenter.default.addObserver(self, selector: #selector(beginUpdateTableVC), name: NSNotification.Name(rawValue: "beginUpdateTableVC"), object: nil)
  35. NotificationCenter.default.addObserver(self, selector: #selector(endUpdateTableVC), name: NSNotification.Name(rawValue: "endUpdateTableVC"), object: nil)
  36. loadDataShkabai.parseTjeratJson {
  37. self.arrayNameCategoryNews = self.loadDataShkabai.arrayNameCategoryNews
  38. self.arrayNewsCategory = self.loadDataShkabai.arrayNewsCategory
  39. self.tableView.reloadData()
  40. }
  41.  
  42. }
  43.  
  44. override func viewWillAppear(_ animated: Bool) {
  45. super.viewWillAppear(animated)
  46. UIView.performWithoutAnimation {
  47. tableView.beginUpdates()
  48. tableView.endUpdates()
  49. }
  50. }
  51.  
  52. override func numberOfSections(in tableView: UITableView) -> Int {
  53. return arrayNewsCategory.count
  54. }
  55.  
  56. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  57. return arrayNewsCategory[section].count
  58. }
  59.  
  60. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  61. return arrayNameCategoryNews[section]
  62. }
  63.  
  64. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  65. let headCell = tableView.dequeueReusableCell(withIdentifier: reuseHeadCellNews, for: indexPath)
  66. //let indicatorCell = tableView.dequeueReusableCell(withIdentifier: reuseIndicCellNews, for: indexPath)
  67.  
  68. if arrayNewsCategory.count != 0{
  69. if let cell = headCell as? NewsTableViewCell{
  70. cell.nameChanelLabel.text = arrayNewsCategory[indexPath.section][indexPath.row].name
  71. if (isSectionOpened(indexPath.section,indexPath.row)) {
  72. cell.symbolCellLabel.text = "⌄"
  73. }else{
  74. cell.symbolCellLabel.text = "⌃"
  75. }
  76. cell.imageChanel.pin_setImage(from: URL(string: arrayNewsCategory[indexPath.section][indexPath.row].logo))
  77.  
  78. if let k = Int(arrayNewsCategory[indexPath.section][indexPath.row].kolNews) {
  79. cell.loadDataNews(url: self.arrayNewsCategory[indexPath.section][indexPath.row].url, numberNews: k, indexPath: indexPath)
  80. }
  81. return cell
  82.  
  83. }
  84. }
  85.  
  86. return headCell
  87. }
  88.  
  89. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  90. changeSectionsStatus(indexPath.section, indexPath.row)
  91. tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
  92. tapCell = indexPath
  93. }
  94.  
  95. override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  96. itemHeights["\(indexPath.section)\(indexPath.row)"] = cell.frame.size.height
  97. }
  98.  
  99. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  100. if isSectionOpened(indexPath.section, indexPath.row) {
  101. return UITableViewAutomaticDimension
  102. }
  103. if let h = itemHeights["\(indexPath.section)\(indexPath.row)"] {
  104. return h
  105. }
  106. return 61
  107. }
  108.  
  109. override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  110. if let h = itemHeights["\(indexPath.section)\(indexPath.row)"] {
  111. tableView.beginUpdates()
  112. tableView.endUpdates()
  113. return h
  114. }
  115. return UITableViewAutomaticDimension
  116. }
  117.  
  118.  
  119. @objc func reloadTableVC(_ notification: NSNotification){
  120. if let index = notification.userInfo![0] as? IndexPath {
  121. if index == tapCell {
  122. //tableView.cellForRow(at: index)?.autoresizingMask
  123. // tableView.beginUpdates()
  124. // tableView.endUpdates()
  125. }
  126.  
  127. }
  128. }
  129.  
  130. @objc func beginUpdateTableVC() {
  131. tableView.beginUpdates()
  132. }
  133.  
  134. @objc func endUpdateTableVC() {
  135. tableView.endUpdates()
  136. }
  137.  
  138. override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
  139. guard let header = view as? UITableViewHeaderFooterView else { return }
  140. header.backgroundView?.backgroundColor = UIColor.white
  141. header.textLabel?.textColor = UIColor.lightGray
  142. header.textLabel?.font = UIFont.boldSystemFont(ofSize: 24)
  143. header.textLabel?.frame = header.frame
  144. }
  145.  
  146. func isSectionOpened(_ section: Int, _ row: Int) -> Bool{
  147. if let status = opensection["\(section)\(row)"]{
  148. return status
  149. }
  150. return false
  151. }
  152.  
  153. func changeSectionsStatus(_ section: Int, _ row: Int){
  154. if let status = opensection["\(section)\(row)"]{
  155. opensection["\(section)\(row)"] = !status
  156. }
  157. else{
  158. opensection["\(section)\(row)"] = true
  159. }
  160. }
  161.  
  162. func closeAllSectionRows(nowSecOpen: String){
  163. if opensection.count != 0{
  164. for some in opensection{
  165. if (nowSecOpen != some.key){
  166. opensection[nowSecOpen] = false
  167. }
  168. }
  169. }
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement