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