【问题标题】:Display user's post on a new ViewController在新的 ViewController 上显示用户的帖子
【发布时间】:2015-09-01 22:10:03
【问题描述】:

我正在开发一个允许用户发布、点赞和评论的应用程序,因此作为我的评论实施的一部分,我正在尝试显示用户的帖子,以便您能够对其发表评论。我正在使用作为主时间线的 UITableView,它显示用户发布的所有帖子,所以我想要做的是,当用户点击一个单元格时,它会显示该单元格的帖子,这样你就可以评论它,但我无法显示它。

到目前为止,这是我用于该实现的代码,那么我缺少什么? 用户单击单元格后,我还能做些什么来在 viewController 中显示所选帖子?

import UIKit
import Parse

class DetailViewContoller: UIViewController, UITableViewDelegate, UITextViewDelegate {

@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var postLabel: UILabel!
@IBOutlet weak var commentTableView: UITableView!

var post: PFObject?
var commentView: UITextView?
var footerView: UIView?
var contentHeight: CGFloat = 0

var comments: [String]?
let FOOTERHEIGHT : CGFloat = 50;

override func viewDidLoad() {
    super.viewDidLoad()

    commentTableView.delegate = self

    /* Setup the keyboard notifications */
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

    if(post?.objectForKey("comments") != nil) {
        comments = post?.objectForKey("comments") as? [String]
    }

    println(post)
    println(post?.objectForKey("content"))
    self.postLabel.text = post?.objectForKey("content") as? String
}


func keyBoardWillShow(notification: NSNotification) {
    var info:NSDictionary = notification.userInfo!
    var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()

    var keyboardHeight:CGFloat =  keyboardSize.height - 40


    var animationDuration:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! CGFloat

    var contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0);
    self.commentTableView.contentInset = contentInsets
    self.commentTableView.scrollIndicatorInsets = contentInsets

}

func keyBoardWillHide(notification: NSNotification) {

    self.commentTableView.contentInset = UIEdgeInsetsZero
    self.commentTableView.scrollIndicatorInsets = UIEdgeInsetsZero
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if let count = comments?.count {
        return count
    }
    return 0
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = commentTableView.dequeueReusableCellWithIdentifier("commentCell", forIndexPath: indexPath) as! CommentTableViewCell
    cell.commentLabel?.text = comments![indexPath.row]
    return cell
}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    if self.footerView != nil {
        return self.footerView!.bounds.height
    }
    return FOOTERHEIGHT
}

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    footerView = UIView(frame: CGRect(x: 0, y: 0, width: commentTableView.bounds.width, height: FOOTERHEIGHT))
    footerView?.backgroundColor = UIColor(red: 243.0/255, green: 243.0/255, blue: 243.0/255, alpha: 1)
    commentView = UITextView(frame: CGRect(x: 10, y: 5, width: commentTableView.bounds.width - 80 , height: 40))
    commentView?.backgroundColor = UIColor.whiteColor()
    commentView?.textContainerInset = UIEdgeInsetsMake(5, 5, 5, 5)
    commentView?.layer.cornerRadius = 2
    commentView?.scrollsToTop = false

    footerView?.addSubview(commentView!)
    let button = UIButton(frame: CGRect(x: commentTableView.bounds.width - 65, y: 10, width: 60 , height: 30))
    button.setTitle("Reply", forState: UIControlState.Normal)
    button.backgroundColor = UIColor(red:  0/0.0, green: 179/255.0, blue: 204/255.0, alpha: 100.0/100.0)
    button.layer.cornerRadius = 5
    button.addTarget(self, action: "reply", forControlEvents: UIControlEvents.TouchUpInside)
    footerView?.addSubview(button)
    commentView?.delegate = self
    return footerView
}


//Hide keyboard after touching background
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    self.view.endEditing(true)
}

//remaining characters
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{

    if (text == "\n") {
        textView.resignFirstResponder()
    }

    return true
}

func reply() {
    post?.addObject(commentView!.text, forKey: "comments")
    post?.saveInBackground()
    if let tmpText = commentView?.text {
        comments?.append(tmpText)
    }
    commentView?.text = ""
    println(comments?.count)
    self.commentView?.resignFirstResponder()
    self.commentTableView.reloadData()
}
}

【问题讨论】:

  • 那么这段代码有什么问题呢?什么不起作用?
  • @rocky 它不会显示您在新视图控制器上单击的帖子,也不会将 cmets 添加到帖子中

标签: ios iphone swift cocoa-touch parse-platform


【解决方案1】:

当您单击表格单元格时,我没有在您的代码中看到任何实际触发的内容。我会修改你的代码的方式是:

  1. 实施override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {}
  2. 使用indexpath 确定您要为其显示详细信息的对象
  3. 为细节创建一个新的视图控制器,将对象传递给它,然后推送它

一种更优雅(但在某些方面复杂)的做事方式是实现PFQueryTableViewController,然后使用函数objectAtIndexPath()检索相关对象并显示详细信息

【讨论】:

  • 我只是有一个围攻,当你点击单元格时,它会将你重定向到那里。我没有为它编写任何代码,只是 ctrl 将单元格拖动到视图控制器。我没有使用PFQueryTableViewController,但我可以使用我为objectAtIndexPath() 实现的方法,但由于我没有围攻的代码,这是在视图控制器中显示帖子的关键部分吗?跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多