[英]strange bug with tableView reload data
I have a tableView with messages. 我有一个带有消息的tableView。 Messages are store into parse.com.
消息存储在parse.com中。
I download asynchronously, put the message in a struct message array 我异步下载,将消息放入结构消息数组中
import UIKit
var messMgr : messageObjet = messageObjet()
var date : NSDate = NSDate()
struct message {
var commentText = ""
var commentDate = ""
}
class messageObjet: NSObject {
var messageData = [message]()
func addMessage(comment : String, date : String) {
//messageData.append(message(commentText: comment, commentDate: date))
var mess = message(commentText: comment, commentDate: date)
messageData.insert(mess, atIndex: 0)
}
}
and populate my tableView 并填充我的tableView
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:CommentTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as CommentTableViewCell
cell.tag = indexPath.row
// Configure the cell...
// check if it is in the cache
println("the messageCache in cellForRow in commentTableViewController is \(self.commentCache.objectForKey(indexPath.row))")
if let messageCached: AnyObject = self.commentCache.objectForKey(indexPath.row){
cell.messageLabel.text = messageCached as? String
}
if let dateCached: AnyObject = self.dateCache.objectForKey(indexPath.row){
cell.dateLabel.text = dateCached as? String
}
else if messMgr.messageData.count != 0 {
if cell.tag == indexPath.row {
cell.messageLabel.text = messMgr.messageData[indexPath.row].commentText
self.commentCache.setObject(cell.messageLabel.text!, forKey: indexPath.row)
cell.dateLabel.text = messMgr.messageData[indexPath.row].commentDate
self.dateCache.setObject(cell.dateLabel.text!, forKey: indexPath.row)
}
}
return cell
}
I have modal viewController to add a new message. 我有模态viewController添加新消息。
In order to display immediately the message after dismissing the modal VC i did this 为了在关闭模态VC之后立即显示消息,我这样做了
@IBAction func sendComment(sender: AnyObject) {
let uuid = userDefaults.stringForKey("ApplicationUniqueIdentifier")
var comment = PFObject(className:"Comment")
comment.setObject(textToSend.text, forKey: "CommentText")
comment.setObject(post, forKey: "Post")
comment.setObject(uuid, forKey: "from")
comment.saveEventually()
let date = NSDate()
newMessage.commentText = textToSend.text as String
newMessage.commentDate = date.relativeTimeToString() as String
messMgr.messageData.insert(newMessage, atIndex: 0)
// messMgr.addMessage(textToSend.text as String, date: date.relativeTimeToString() as String)
NSNotificationCenter.defaultCenter().postNotificationName("reloadMessageTableView", object: nil)
println(messMgr.messageData)
self.dismissViewControllerAnimated(true, completion: nil)
}
The problem is when i come back to my tableView the message added to the index 0 is always displayed as the previous message and when i print my message array the index 0 message is the good one.. 问题是当我回到tableView时,添加到索引0的消息始终显示为前一条消息,而当我打印我的消息数组时,索引0的消息就是好消息。
Any idea ? 任何想法 ?
好的问题是,我将消息存储在NSCache中,并使用indexPath作为键...删除了此消息,everythings正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.