아이폰의 메시지 앱에 대한 초기보기처럼 보낸 사람 또는 보낸 사람의 마지막 메시지를 사용하여 내 사용자의 테이블 뷰를 채우려고합니다. 이 코드를 가지고 있지만 그것은 내가 사용자에게 전송하고 예상 된 결과 반환 된 모든 메시지에 스냅 샷을 유지 사전의 배열을 통해 반복 작동하지 않습니다Swift의 SnapShot 값으로 TableViewCell 채우기
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
func getMsg()-> Int{
var getIndex = Int()
let messageDb = Database.database().reference().child("iMessenger/Messages")
messageDb.observe(.childAdded) { (snapshot) in
if let snapshotvalue = snapshot.value as? [String:Any], let sender = snapshotvalue["sender"] as? String, let key = snapshotvalue["key"] as? String{
// I thought that I could iterate through my array of dictionaries taking care to find a matching sender value for the user at indexpath.row and the key for the message
for (index, dict) in newMessageDictArr.enumerated(){
if self.users[indexPath.row].userPhone == sender && dict["key"] == key{getIndex = index}}}}
return getIndex}
if newMessageDictArr.count != 0{
cell.userMessage.text = newMessageDictArr[getMsg()]["messageBody"]}
return cell}
난 문제를 코드와 함께있는 것은
cell.userMessage.text = newMessageDictArr[getMsg()]["messageBody"]
내 모든 세포가 [ "messageBody"]의 첫 번째 값이 너무 내 사용자 나에게 동일한 메시지를 보낸 듯 모든처럼 만드는 것입니다. 내가 어디로 잘못 갔는지 알아 내려고 노력했습니다 ...
또한 여기 내 FireBase 구조, 감사합니다.
{
"iMessenger" : {
"Messages" : {
"-KxDSQOBuCkpFdw4SPDh" : {
"messageBody" : "TEST ",
"receiver" : "+197862",
"sender" : "+16698"
},
"-KxBjNM_iA2XaapyyC9o" : {
"key" : "-KxBjNM_iA2XaapyyC9o",
"messageBody" : "TEST",
"receiver" : "+197862",
"sender" : "+1914862"
},
"Messages+199862+197862" : {
"-KxB7P-vgdfxRpSdnwCT" : {
"messageBody" : "yo, wassup world",
"receiver" : "+197862",
"sender" : "+199862"
},
"-KxB81fbn5_OHxdj4lcA" : {
"messageBody" : "Hello World",
"receiver" : "+19147862",
"sender" : "+1997862"
}
},
"Users" : {
"-Kx41o03NIMBR68F2sCS" : {
"displayName" : "sleeping beauty ",
"firstName" : "",
"lastName" : "",
"phoneNumber" : "+165698"
},
"-Kx42IXgN1z9D5Zoz0fk" : {
"displayName" : "KarmaDeli",
"firstName" : "",
"lastName" : "",
"phoneNumber" : "+1397862"
}}}}
이 코드는 올바르게 작동하지 않을 수 있으며 이상한 UI 문제가 발생할 수 있으며 결국에는 많은 작업이 필요할 수 있습니다. 개념은 노드에 옵저버를 부착하고 앱이 데이터가 추가, 변경 또는 제거 된 이벤트를 받으면 그에 따라 tableView 데이터 소스 (일반적으로 배열)를 업데이트 한 다음 tableView를 다시로드하여 해당 배열에서 테이블을 채 웁니다. tableView : cellForRowAt 메서드 내에서 옵저버를 추가하고 싶지는 않습니다. 질문은 그들이 보내거나 보낸 마지막 메시지로 채워질 것이라고 명시합니다. 말 그대로 당신이 tableView에 하나의 메시지가 있다는 것을 의미합니까? – Jay
아, 파이어베이스 구조를 텍스트로 추가하십시오. 이미지가 없으므로 제대로 답변을 작성할 수 있습니다. Firebase 콘솔 -> JSON 내보내기를 통해 Firebase 구조를 가져올 수 있습니다. 작은 스 니펫 만 포함하면 쿼리하려는 내용을 이해할 수 있습니다. (텍스트로 포함!) – Jay
그래요, tableview에서 스냅 샷을 찍는 것은 나쁜 생각이었습니다. 내 tableView는 현재 2 개의 fireBase 위치에서 데이터를 가져옵니다. iMessenger/Users 및 iMessenger/Messages. 한 데이터베이스에서만 tableview를 채울 수있는 방법을 찾아야합니까? 나가 의미 한 무엇을 나가 나의 전화에 app를 달릴 때 얼마나 많은 새로운 메시지가 보내지는지 상관없이 그 tableVIew에 영원히 사용자를위한 cell.message.text에있는 첫번째 messageBody [ "Hello world"]를 본 때이었다. – KarmaDeli