tableView重新加载数据的奇怪错误

时间:2014-12-18 18:02:11

标签: ios arrays uitableview swift

我有一个带有消息的tableView。 消息存储在parse.com中。

我以异步方式下载,将消息放入struct消息数组

    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)
   }


 }

并填充我的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
}

我有模态viewController来添加新消息。

为了在解除模态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)

}

问题是当我回到我的tableView时,添加到索引0的消息总是显示为上一条消息,当我打印我的消息数组时,索引0消息是好的...

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好的问题是我将消息存储在NSCache中,并将indexPath作为键... 删除了这个并且每个部分工作都很好。