BBSSubjectCreateTableViewController.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // BBSSubjectCreateTableViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2016/11/17.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireObjectMapper
  11. import ObjectMapper
  12. import WebKit
  13. import CocoaLumberjack
  14. class BBSSubjectCreateTableViewController: UITableViewController {
  15. let subjectcategory = ["讨论","新闻","灌水","知识","动态"]
  16. var sectionData:BBSectionListData?
  17. @IBOutlet weak var sectionNameLabel: UILabel!
  18. @IBOutlet weak var subjectCategoryPickView: UIPickerView!
  19. @IBOutlet weak var subjectTextField: UITextField!
  20. @IBOutlet weak var descTextField: UITextField!
  21. @IBOutlet weak var contentContainerView: UIView!
  22. var myHtmlContent:String?
  23. var pushlishEntity = PublishSubjectEntity()
  24. @IBOutlet weak var myWkWebView: WKWebView!
  25. required init?(coder aDecoder: NSCoder) {
  26. super.init(coder: aDecoder)
  27. }
  28. override func viewWillAppear(_ animated: Bool) {
  29. super.viewWillAppear(animated)
  30. }
  31. override func viewDidAppear(_ animated: Bool) {
  32. super.viewDidAppear(animated)
  33. }
  34. override func viewWillDisappear(_ animated: Bool) {
  35. super.viewWillDisappear(animated)
  36. }
  37. override func viewDidDisappear(_ animated: Bool) {
  38. super.viewDidDisappear(animated)
  39. }
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. self.pushlishEntity.sectionId = self.sectionData?.id
  43. self.pushlishEntity.type = self.subjectcategory[0]
  44. self.sectionNameLabel.text = self.sectionData?.sectionName
  45. self.subjectCategoryPickView.dataSource = self
  46. self.subjectCategoryPickView.delegate = self
  47. self.subjectTextField.delegate = self
  48. self.descTextField.delegate = self
  49. loadHtmlToWebView()
  50. }
  51. func loadHtmlToWebView(){
  52. self.myWkWebView.loadHTMLString(myHtmlContent == nil ? "" : myHtmlContent!, baseURL: nil)
  53. }
  54. override func didReceiveMemoryWarning() {
  55. super.didReceiveMemoryWarning()
  56. // Dispose of any resources that can be recreated.
  57. }
  58. // MARK: - Table view data source
  59. override func numberOfSections(in tableView: UITableView) -> Int {
  60. // #warning Incomplete implementation, return the number of sections
  61. return 2
  62. }
  63. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  64. // #warning Incomplete implementation, return the number of rows
  65. switch section {
  66. case 0:
  67. return 4
  68. case 1:
  69. return 1
  70. default:
  71. return 0
  72. }
  73. }
  74. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  75. switch indexPath.section {
  76. case 0:
  77. return 50.0
  78. case 1:
  79. return 280.0
  80. default:
  81. return 50.0
  82. }
  83. }
  84. override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  85. let headerView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 40))
  86. if section == 1 {
  87. let button = UIButton(type: .custom)
  88. let attributes = [NSAttributedString.Key.font:UIFont(name: "PingFangSC-Regular", size: 14.0)!,NSAttributedString.Key.foregroundColor:UIColor.white]
  89. let attrString = NSAttributedString(string: "点击编辑正文内容", attributes: attributes)
  90. button.setAttributedTitle(attrString, for: .normal)
  91. button.theme_backgroundColor = ThemeColorPicker(keyPath: "Base.base_color")
  92. button.frame = CGRect(x: 10, y: 5, width: 150, height: 30)
  93. button.addTarget(self, action: #selector(showEditControlAction(_:)), for: .touchUpInside)
  94. headerView.addSubview(button)
  95. }else{
  96. let label = UILabel(frame: CGRect(x: 10, y: 5, width: 150, height: 30))
  97. label.text = "发帖信息"
  98. label.font = UIFont(name: "PingFangSC-Regular", size: 14.0)!
  99. label.textColor = RGB(18, g: 18, b: 18)
  100. headerView.addSubview(label)
  101. }
  102. return headerView
  103. }
  104. @IBAction func publishSubjectAction(_ sender: UIBarButtonItem) {
  105. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(BBSContext.bbsContextKey, query: BBSContext.itemCreateQuery, parameter: nil)
  106. AF.request(url!, method: .post, parameters: pushlishEntity.toJSON(), encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
  107. switch response.result {
  108. case .success(let val):
  109. let type = JSON(val)["type"]
  110. if type == "success" {
  111. DispatchQueue.main.async {
  112. self.showSuccess(title: "发帖成功")
  113. self.performSegue(withIdentifier:"backSectionListSegue", sender: nil)
  114. }
  115. }else{
  116. DDLogError(JSON(val).description)
  117. DispatchQueue.main.async {
  118. self.showError(title: "发帖失败")
  119. }
  120. }
  121. case .failure(let err):
  122. DDLogError(err.localizedDescription)
  123. DispatchQueue.main.async {
  124. self.showError(title: "发帖失败")
  125. }
  126. }
  127. }
  128. }
  129. @IBAction func unBackEditContentAction(_ segue:UIStoryboardSegue){
  130. loadHtmlToWebView()
  131. }
  132. @IBAction func showEditControlAction(_ sender: UIButton) {
  133. self.performSegue(withIdentifier: "editSubjectContentSegue", sender: nil)
  134. }
  135. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  136. if segue.identifier == "editSubjectContentSegue" {
  137. let navVC = segue.destination as! ZLNavigationController
  138. let destVC = navVC.topViewController as! BBSSubjectContentViewController
  139. destVC.backDelegate = self
  140. destVC.myContentHTML = self.myHtmlContent
  141. }
  142. }
  143. }
  144. extension BBSSubjectCreateTableViewController:UITextFieldDelegate{
  145. func textFieldDidEndEditing(_ textField: UITextField) {
  146. if textField.isEqual(subjectTextField){
  147. self.pushlishEntity.title = self.subjectTextField.text
  148. }else if textField.isEqual(descTextField) {
  149. self.pushlishEntity.summary = descTextField.text
  150. }
  151. }
  152. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  153. if textField.isEqual(subjectTextField){
  154. descTextField.becomeFirstResponder()
  155. }else if textField.isEqual(descTextField) {
  156. self.dismissKeyboard()
  157. }
  158. return true
  159. }
  160. }
  161. extension BBSSubjectCreateTableViewController:UIPickerViewDataSource,UIPickerViewDelegate{
  162. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  163. return 1
  164. }
  165. func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  166. return self.subjectcategory.count
  167. }
  168. func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
  169. let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 120, height: 30))
  170. let title = self.subjectcategory[row]
  171. titleLabel.text = title
  172. titleLabel.theme_textColor = ThemeColorPicker(keyPath: "Base.base_color")
  173. titleLabel.textAlignment = .left
  174. titleLabel.font = UIFont(name: "PingFangSC-Regular", size: 14.0)!
  175. titleLabel.backgroundColor = UIColor.clear
  176. return titleLabel
  177. }
  178. func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  179. let title = self.subjectcategory[row]
  180. DDLogDebug(title)
  181. self.pushlishEntity.type = title
  182. }
  183. func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
  184. return 120.0
  185. }
  186. }
  187. extension BBSSubjectCreateTableViewController:SubjectContentEditBackDelegate{
  188. func backEditContent(contentHtml: String) {
  189. self.myHtmlContent = contentHtml
  190. self.pushlishEntity.content = contentHtml
  191. self.loadHtmlToWebView()
  192. }
  193. }