123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- //
- // BBSSubjectCreateTableViewController.swift
- // O2Platform
- //
- // Created by 刘振兴 on 2016/11/17.
- // Copyright © 2016年 zoneland. All rights reserved.
- //
- import UIKit
- import Alamofire
- import AlamofireObjectMapper
- import ObjectMapper
- import WebKit
- import CocoaLumberjack
- class BBSSubjectCreateTableViewController: UITableViewController {
-
- let subjectcategory = ["讨论","新闻","灌水","知识","动态"]
-
- var sectionData:BBSectionListData?
-
- @IBOutlet weak var sectionNameLabel: UILabel!
-
- @IBOutlet weak var subjectCategoryPickView: UIPickerView!
- @IBOutlet weak var subjectTextField: UITextField!
-
- @IBOutlet weak var descTextField: UITextField!
-
- @IBOutlet weak var contentContainerView: UIView!
-
- var myHtmlContent:String?
-
- var pushlishEntity = PublishSubjectEntity()
-
-
- @IBOutlet weak var myWkWebView: WKWebView!
-
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
-
-
- }
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- }
-
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- }
-
-
- override func viewDidDisappear(_ animated: Bool) {
- super.viewDidDisappear(animated)
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- self.pushlishEntity.sectionId = self.sectionData?.id
- self.pushlishEntity.type = self.subjectcategory[0]
- self.sectionNameLabel.text = self.sectionData?.sectionName
- self.subjectCategoryPickView.dataSource = self
- self.subjectCategoryPickView.delegate = self
- self.subjectTextField.delegate = self
- self.descTextField.delegate = self
- loadHtmlToWebView()
- }
-
- func loadHtmlToWebView(){
- self.myWkWebView.loadHTMLString(myHtmlContent == nil ? "" : myHtmlContent!, baseURL: nil)
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- // MARK: - Table view data source
- override func numberOfSections(in tableView: UITableView) -> Int {
- // #warning Incomplete implementation, return the number of sections
- return 2
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- // #warning Incomplete implementation, return the number of rows
- switch section {
- case 0:
- return 4
- case 1:
- return 1
- default:
- return 0
- }
- }
-
- override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- switch indexPath.section {
- case 0:
- return 50.0
- case 1:
- return 280.0
- default:
- return 50.0
- }
- }
-
- override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- let headerView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 40))
- if section == 1 {
- let button = UIButton(type: .custom)
- let attributes = [NSAttributedString.Key.font:UIFont(name: "PingFangSC-Regular", size: 14.0)!,NSAttributedString.Key.foregroundColor:UIColor.white]
- let attrString = NSAttributedString(string: "点击编辑正文内容", attributes: attributes)
- button.setAttributedTitle(attrString, for: .normal)
- button.theme_backgroundColor = ThemeColorPicker(keyPath: "Base.base_color")
- button.frame = CGRect(x: 10, y: 5, width: 150, height: 30)
- button.addTarget(self, action: #selector(showEditControlAction(_:)), for: .touchUpInside)
- headerView.addSubview(button)
- }else{
- let label = UILabel(frame: CGRect(x: 10, y: 5, width: 150, height: 30))
- label.text = "发帖信息"
- label.font = UIFont(name: "PingFangSC-Regular", size: 14.0)!
- label.textColor = RGB(18, g: 18, b: 18)
- headerView.addSubview(label)
- }
- return headerView
- }
-
-
- @IBAction func publishSubjectAction(_ sender: UIBarButtonItem) {
- let url = AppDelegate.o2Collect.generateURLWithAppContextKey(BBSContext.bbsContextKey, query: BBSContext.itemCreateQuery, parameter: nil)
- AF.request(url!, method: .post, parameters: pushlishEntity.toJSON(), encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
- switch response.result {
- case .success(let val):
- let type = JSON(val)["type"]
- if type == "success" {
- DispatchQueue.main.async {
- self.showSuccess(title: "发帖成功")
- self.performSegue(withIdentifier:"backSectionListSegue", sender: nil)
- }
- }else{
- DDLogError(JSON(val).description)
- DispatchQueue.main.async {
- self.showError(title: "发帖失败")
- }
- }
- case .failure(let err):
- DDLogError(err.localizedDescription)
- DispatchQueue.main.async {
- self.showError(title: "发帖失败")
- }
- }
- }
- }
-
- @IBAction func unBackEditContentAction(_ segue:UIStoryboardSegue){
- loadHtmlToWebView()
- }
-
-
- @IBAction func showEditControlAction(_ sender: UIButton) {
- self.performSegue(withIdentifier: "editSubjectContentSegue", sender: nil)
- }
-
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- if segue.identifier == "editSubjectContentSegue" {
- let navVC = segue.destination as! ZLNavigationController
- let destVC = navVC.topViewController as! BBSSubjectContentViewController
- destVC.backDelegate = self
- destVC.myContentHTML = self.myHtmlContent
- }
- }
-
- }
- extension BBSSubjectCreateTableViewController:UITextFieldDelegate{
- func textFieldDidEndEditing(_ textField: UITextField) {
- if textField.isEqual(subjectTextField){
- self.pushlishEntity.title = self.subjectTextField.text
- }else if textField.isEqual(descTextField) {
- self.pushlishEntity.summary = descTextField.text
- }
- }
-
- func textFieldShouldReturn(_ textField: UITextField) -> Bool {
- if textField.isEqual(subjectTextField){
- descTextField.becomeFirstResponder()
- }else if textField.isEqual(descTextField) {
- self.dismissKeyboard()
- }
- return true
- }
- }
- extension BBSSubjectCreateTableViewController:UIPickerViewDataSource,UIPickerViewDelegate{
- func numberOfComponents(in pickerView: UIPickerView) -> Int {
- return 1
- }
-
- func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
- return self.subjectcategory.count
- }
-
- func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
- let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 120, height: 30))
- let title = self.subjectcategory[row]
- titleLabel.text = title
- titleLabel.theme_textColor = ThemeColorPicker(keyPath: "Base.base_color")
- titleLabel.textAlignment = .left
- titleLabel.font = UIFont(name: "PingFangSC-Regular", size: 14.0)!
- titleLabel.backgroundColor = UIColor.clear
- return titleLabel
- }
-
- func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
- let title = self.subjectcategory[row]
- DDLogDebug(title)
- self.pushlishEntity.type = title
- }
-
- func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
- return 120.0
- }
- }
- extension BBSSubjectCreateTableViewController:SubjectContentEditBackDelegate{
- func backEditContent(contentHtml: String) {
- self.myHtmlContent = contentHtml
- self.pushlishEntity.content = contentHtml
- self.loadHtmlToWebView()
- }
- }
|