在 Xcode 中使用 Json Wordpress 发表评论

Posted

技术标签:

【中文标题】在 Xcode 中使用 Json Wordpress 发表评论【英文标题】:Leave A Comment With Json Wordpress in Xcode 【发布时间】:2020-04-19 10:49:00 【问题描述】:

我使用我的 Wordpress 后端在 swift 中使用 Xcode 制作了应用程序并使用 json 获取数据,我可以向用户显示我的应用程序的评论,但我希望用户也可以在应用程序中留下 cmets,应该有一个选项用于发表评论在帖子详细信息文件中,这是我显示 cmets 的代码

 @IBAction func commentViewController(_ sender: Any) 
        let vc = CommentViewController()
        vc.dataArray = jsonData["comments"].array
        self.navigationController!.pushViewController(vc, animated: true)
    

 self.commentButton.layer.borderWidth = 2
        self.commentButton.layer.borderColor = baseColor.cgColor
        self.commentButton.layer.cornerRadius = 4.0
        self.commentButton.tintColor = baseColor
        self.commentButton.setTitle("comments(\(jsonData["comments"].array?.count ?? 0))", for: .normal)

但我不知道如何制作评论框,用户也可以发表评论,谢谢

这是我的 CommentViewController:

 var tableView: UITableView  =   UITableView()
var dataArray: Array<JSON>!
override func viewDidLoad() 
    super.viewDidLoad()

    self.title = "Comments"

    if #available(ios 11.0, *) 
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationController?.navigationItem.largeTitleDisplayMode = .automatic

        let attributes = [
            NSAttributedString.Key.foregroundColor : navigationBarTextColor, NSAttributedString.Key.font: UIFont(name: "SFUIText-Medium", size: 34),
            ]

        navigationController?.navigationBar.largeTitleTextAttributes = attributes as [NSAttributedString.Key : Any]

     else 
        // Fallback on earlier versions
    

    // Check if Post have comments
    if(self.dataArray.count == 0)
        self.view.backgroundColor = UIColor(red: 216 / 255, green: 216 / 255, blue: 216 / 255, alpha: 1.0)
        let label = UILabel(frame: CGRect(x: (UIScreen.main.bounds.width/2)-100, y: (UIScreen.main.bounds.height/2)-50, width: 200, height: 50))
        label.text = "No Comments"
        label.font = UIFont(name: "SFUIText-Regular", size: 18)
        label.textAlignment = NSTextAlignment.center
        self.view.addSubview(label)
    
    else
       self.setupTable()
    


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.



func getDateFromString(String:String) -> String
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    let dateObj = dateFormatter.date(from: String)
    dateFormatter.dateFormat = "yyyy-MM-dd"
    return dateFormatter.string(from: dateObj!)



func setupTable()
    tableView = UITableView(frame: UIScreen.main.bounds, style: UITableView.Style.plain)
    tableView.delegate      =   self
    tableView.dataSource    =   self
    tableView.register(UINib(nibName: "commentViewCell", bundle: nil), forCellReuseIdentifier: "cell")
    tableView.frame.size.height = UIScreen.main.bounds.height - 64
    tableView.bounces = false
    tableView.allowsSelection = false
    self.view.addSubview(self.tableView)


func numberOfSections(in tableView: UITableView) -> Int 
    return 1


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

  return self.dataArray.count
 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! commentViewCell
    cell.commentText.text = String(htmlEncodedString: dataArray[indexPath.row]["content"].stringValue)
    cell.name.text = String(htmlEncodedString:dataArray[indexPath.row]["name"].stringValue)
    cell.date.text = getDateFromString(String: dataArray[indexPath.row]["date"].stringValue)

    return cell


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
    return UITableView.automaticDimension


func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat 
    return UITableView.automaticDimension

【问题讨论】:

如果这真的是你要问的,你需要一个 UITextField 或 UITextView,这个问题相当模糊。 你的 CommentViewController 是什么样子的? 我已经编辑了问题,请查看@OlhaPavliuk 【参考方案1】:

所以你有一个显示所有 cmets 的“tableView”字段。 正如Joakim Danielson 的建议,您应该将 UITextView 添加到屏幕底部。

由于表格视图可能太长而无法适应屏幕高度,它可能会滚动,因此将此文本视图添加为单元格会更容易。

所以你可以通过以下方式做到这一点:

    更改tableView的大小以在最后留下评论:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

  return self.dataArray.count + 1

    添加带有文本视图的单元格,我们将其命名为LeaveCommentCell

    LeaveCommentCell 实例作为最后一个单元格返回:


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

    if indexPath.row == self.dataArray.count   // this is the last row
        let cell = tableView.dequeueReusableCell(withIdentifier: "AddCommentCell", for: indexPath) as! LeaveCommentCell // your new cell

        return cell
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "ShowCommentCell", for: indexPath) as! commentViewCell // please note identifiers should differ 
    cell.commentText.text = String(htmlEncodedString: dataArray[indexPath.row]["content"].stringValue)
    cell.name.text = String(htmlEncodedString:dataArray[indexPath.row]["name"].stringValue)
    cell.date.text = getDateFromString(String: dataArray[indexPath.row]["date"].stringValue)

    return cell


    LeaveCommentCell 添加一个按钮(例如带有文本“发布”),在按下它时订阅事件,获取评论文本并将其发送到服务器。

【讨论】:

以上是关于在 Xcode 中使用 Json Wordpress 发表评论的主要内容,如果未能解决你的问题,请参考以下文章

实战Wordpres的CSRF漏洞利用

HTML 在wordpres强制换行

询问在 xcode4 中使用 json

在 Xcode 中使用 Json Wordpress 发表评论

ACF wordpres: Foreach: data-type = 'group'

Wordpres标头背景图片