2016-11-20 124 views
0

我正尝试使用tableview创建一个聊天应用程序来显示消息。一切正常,除了一些单元格不会显示。它并不总是相同的细胞。我从我的Firebase数据库中收到消息。某些单元格不会显示

我的代码:

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

    if let cell = cellCache.object(forKey: indexPath as AnyObject) as? UITableViewCell{ 
     return cell 
    } 

    let cell = messagesTableView.dequeueReusableCell(withIdentifier: "otherMessageCell") as! OtherMessageTableViewCell 

    DispatchQueue.global(qos: .userInteractive).async { 

     let message = self.messages[indexPath.row] 
     let ref = FIRDatabase.database().reference() 

     let uid = FIRAuth.auth()?.currentUser?.uid 

     if(uid == message.sender) { 
      cell.sender.textAlignment = .right 
      cell.message.textAlignment = .right 
     }else{ 
      cell.sender.textAlignment = .left 
      cell.message.textAlignment = .left 
     } 

     let uidReference = ref.child("Users").child(message.sender!) 

     uidReference.observeSingleEvent(of: .value, with: { (snapshot) in 

      if let dictionary = snapshot.value as? [String: AnyObject] { 
       let username = dictionary["Username"] as! String 
       let imageLink = dictionary["Image Link"] as! String 

       cell.sender.text = username 
       cell.message.text = message.message 
       cell.profileImage.image = nil 
       cell.profileImage.loadImageWithURLString(urlString: imageLink) 
      } 

     }, withCancel: nil) 
    } 

    DispatchQueue.main.async { 
     self.cellCache.setObject(cell, forKey: indexPath as AnyObject) 
    } 

    return cell 
} 

例子: enter image description here

我希望有人能够帮助我。由于

+0

您的代码正在积极地打击(可能会打破)框架。 DispatchQueue调用和这个'cellCache'的thingamabob都是不必要的,会干扰正确的单元重用。 – vzsg

回答

0

我已经找到了解决办法:

var savedSenders = [Int: String]() 
var savedMessages = [Int: String]() 
var savedImages = [Int: UIImage]() 
var savedAlignments = [Int: NSTextAlignment]() 

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

    let cell = messagesTableView.dequeueReusableCell(withIdentifier: "otherMessageCell") as! OtherMessageTableViewCell 

    if(savedSenders[indexPath.row] != nil && savedMessages[indexPath.row] != nil && savedImages[indexPath.row] != nil && savedAlignments[indexPath.row] != nil) { 
     cell.sender.textAlignment = savedAlignments[indexPath.row]! 
     cell.message.textAlignment = savedAlignments[indexPath.row]! 

     cell.sender.text = savedSenders[indexPath.row] 
     cell.message.text = savedMessages[indexPath.row] 
     cell.profileImage.image = savedImages[indexPath.row] 

     return cell 
    } 

    cell.sender.text = "" 
    cell.message.text = "" 
    cell.profileImage.image = nil 

    DispatchQueue.global(qos: .userInteractive).async { 

     let message = self.messages[indexPath.row] 
     let ref = FIRDatabase.database().reference() 

     let uid = FIRAuth.auth()?.currentUser?.uid 

     if(uid == message.sender) { 
      cell.sender.textAlignment = .right 
      cell.message.textAlignment = .right 
      self.savedAlignments.updateValue(.right, forKey: indexPath.row) 
     }else{ 
      cell.sender.textAlignment = .left 
      cell.message.textAlignment = .left 
      self.savedAlignments.updateValue(.left, forKey: indexPath.row) 
     } 

     let uidReference = ref.child("Users").child(message.sender!) 

     uidReference.observeSingleEvent(of: .value, with: { (snapshot) in 

      if let dictionary = snapshot.value as? [String: AnyObject] { 
       let username = dictionary["Username"] as! String 
       let imageLink = dictionary["Image Link"] as! String 

       cell.sender.text = username 
       cell.message.text = message.message 
       cell.profileImage.image = nil 
       cell.profileImage.loadImageWithURLString(urlString: imageLink) 

        let url = URL(string: imageLink) 
        URLSession.shared.dataTask(with: url!) { (data, response, error) in 

         if(error != nil){ 
          print(error as Any) 
          return 
         } 

         DispatchQueue.main.async { 
          if let downloadedImage = UIImage(data: data!) { 
           let image = downloadedImage 

           self.savedSenders.updateValue(username, forKey: indexPath.row) 
           self.savedMessages.updateValue(message.message!, forKey: indexPath.row) 
           self.savedImages.updateValue(image, forKey: indexPath.row) 
          } 
         } 
        }.resume() 
      } 

     }, withCancel: nil) 
    } 

    return cell 
} 

对每个单元中的数据保存到阵列(savedSenders,savedMessages,savedImages和savedAlignments)。如果我没有将它保存到数组中,单元格将不得不加载来自Firebase的所有数据,这将花费更长的时间。如果需要更长时间,它会看起来不太好。 我测试了一切,它的工作原理。