2016-11-20 37 views
0

私はメッセージを表示するためにchatviewアプリケーションを作成しようとしています。いくつかのセルが表示されないことを除いて、すべてが正常に動作します。必ずしも同じ細胞であるとは限りません。私は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からすべてのデータをロードする必要があり、これは時間がかかります。時間がかかりますと、見栄えが悪くなります。 私はすべてをテストして動作します。

関連する問題