无法在 swift 中显示单元格日期,没有错误
Trouble displaying Cell Date in swift, No errors
好的,所以基本上是使用解析设置基本的消息传递应用程序,在这里完成我的消息控制器,但单元格数据没有显示。我没有收到任何错误,所以没有什么可继续的。有眼力的人能看出错误代码如下:-
class MessagesTableViewController: UITableViewController {
var messages = []
override func viewWillAppear(animated: Bool) {
messages = []
self.navigationController?.title = "My Messages"
var currentUser = PFUser.currentUser()?.username
var query = PFQuery(className: "toUser")
query.whereKey("toUser", equalTo: currentUser!)
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock({ (results: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
self.messages = results!
self.tableView.reloadData()
}
})
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return messages.count > 0 ? 1 : 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
let messageDate = messages[indexPath.row].valueForKey("createdAt") as! NSDate
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyy hh:mm:ss"
let strDate = dateFormatter.stringFromDate(messageDate)
let message: String = messages[indexPath.row].valueForKey("message") as! String
let fromUser: String = messages[indexPath.row].valueForKey("fromUser") as! String
cell.textLabel?.text = message
cell.detailTextLabel?.text = "From \(fromUser) : \(strDate) "
return cell
}
}
有什么想法吗?
谢谢
我这样试过,重装也没关系,我想dataSource.It可能是数据的问题。
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
swift_dispatch_after(2, { () -> () in
self.array = NSArray(objects:"a","bb","vcc")
self.tableView.reloadData()
});
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
if array.count != 0 {
let messageDate = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyy hh:mm:ss"
let strDate = dateFormatter.stringFromDate(messageDate)
cell.textLabel?.text = (array[indexPath.row] as! String) + strDate
}
return cell
}
好吧,这表明长时间看代码会伤到你的眼睛!由于一个简单的错误,数据没有通过
这一行:-
var query = PFQuery(className: "message")
我最初将其设置为
var query = PFQuery(className: "toUser")
我的数据中有错误的 table 名称
感谢所有看过这篇文章的人
好的,所以基本上是使用解析设置基本的消息传递应用程序,在这里完成我的消息控制器,但单元格数据没有显示。我没有收到任何错误,所以没有什么可继续的。有眼力的人能看出错误代码如下:-
class MessagesTableViewController: UITableViewController {
var messages = []
override func viewWillAppear(animated: Bool) {
messages = []
self.navigationController?.title = "My Messages"
var currentUser = PFUser.currentUser()?.username
var query = PFQuery(className: "toUser")
query.whereKey("toUser", equalTo: currentUser!)
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock({ (results: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
self.messages = results!
self.tableView.reloadData()
}
})
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return messages.count > 0 ? 1 : 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
let messageDate = messages[indexPath.row].valueForKey("createdAt") as! NSDate
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyy hh:mm:ss"
let strDate = dateFormatter.stringFromDate(messageDate)
let message: String = messages[indexPath.row].valueForKey("message") as! String
let fromUser: String = messages[indexPath.row].valueForKey("fromUser") as! String
cell.textLabel?.text = message
cell.detailTextLabel?.text = "From \(fromUser) : \(strDate) "
return cell
}
}
有什么想法吗? 谢谢
我这样试过,重装也没关系,我想dataSource.It可能是数据的问题。
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
swift_dispatch_after(2, { () -> () in
self.array = NSArray(objects:"a","bb","vcc")
self.tableView.reloadData()
});
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
if array.count != 0 {
let messageDate = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyy hh:mm:ss"
let strDate = dateFormatter.stringFromDate(messageDate)
cell.textLabel?.text = (array[indexPath.row] as! String) + strDate
}
return cell
}
好吧,这表明长时间看代码会伤到你的眼睛!由于一个简单的错误,数据没有通过
这一行:-
var query = PFQuery(className: "message")
我最初将其设置为
var query = PFQuery(className: "toUser")
我的数据中有错误的 table 名称
感谢所有看过这篇文章的人